'Unit Test in Django couldn't find the created object for editing

I have a tests.py where I'm creating a course->unit>workspace. I need to pass the pk of workspace and save the answer. However, pk =1 doesn't exist, it returns me 404 -Not Found

class CreateStudentCourseTests(APITestCase):  

    @classmethod
    def setUpTestData(cls):
        author = User.objects.create(username="+996555446065", author=True, student=False, email='[email protected]',
                                   subscription_status='is_paid')
        category = Category.objects.create(title='Разработка и IT, Development and IT')
        course = Course.objects.create(title="Разработка на Java", price=1000,
                                       description="Мы научим программировать",
                                       user_id=author.pk, status='published', is_open_moderator=False, is_open_author=False)
        course.category.add(category.pk)

        unit = Units.objects.create(title="Типы данных в Java", description="Первая глава", ord=1, course_id=1, is_free=False)
        
        workspace = Workspace.objects.create(unit=unit, ord=1, title='Первый экран', answer_choices={"answer_choices_question1":"Какое тесто используется для манты?", "answer1":"дрожевое",
                         "answer2":"обычное", "correct_message":"Вы правы", "wrong_answer":"Не так", "correct":"обычное"}, open_answer={"open_answer_question1":"Расскажите о вашем опыте?", "answer1":""},
                         yes_no={"yes_no_question1":"Вам все понравилось?", "yes":"", "no":""})
                         
        student = User.objects.create(username="+996550102030", student=True, author=False)

    def setUp(self):
        self.token = Token.objects.get(user=2)
        self.api_authentication()
        self.data = {
            "start": True,
            "course":1,            
        }

    def api_authentication(self):
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + str(self.token))
    
   def test_save_answers_course(self):
        response = self.client.post('/api/v1/student_course/save/students/answers/1/', data={
            "workspace":1, 
            "answer_choices":{"answer_choices_answer1":"электр"},
            "open_answer":{"open_answer_answer1":"тест для ответов 12"},
            "yes_no":{"yes_no_answer1":True}            
        }, format='json')
        print('response', response)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(UnitData.objects.filter(workspace=1).answer_choices, {"answer_choices_answer1":"электр"})

Answer is:

Traceback (most recent call last):
  File "/home/ainura/Desktop/edengine-units-api/student_course/tests.py", line 76, in test_save_answers_course
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)
AssertionError: 404 != 201


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source