У меня есть несколько простых классов: Visitor, Student и Visit
Модель Visit выглядит следующим образом:
class Visit(models.Model):
visitor = models.ForeignKey(Visitor, on_delete=models.PROTECT)
students = models.ManyToManyField(Student, blank=True)
check_in = models.DateTimeField(auto_now_add=True)
check_out = models.DateTimeField(null=True, blank=True)
def __str__(self):
return "%s : %s" % (self.visitor, self.students)
class Student(models.Model):
visitors = models.ManyToManyField(Visitor, blank=True)
first_name = models.CharField(max_length=40)
last_name = models.CharField(max_length=40)
def __str__(self):
return "%s, %s" % (self.last_name, self.first_name)
class Visitor(models.Model):
TYPE_CHOICES = (
('P', 'Parent'),
('O', 'Other Parent'),
)
type = models.CharField(choices=TYPE_CHOICES, max_length=1)
first_name = models.CharField(max_length=40, validators=[alphaonly])
last_name = models.CharField(max_length=40, validators=[alphaonly])
birth_date = models.DateField(null=True, blank=True)
def __str__(self):
return "%s, %s" % (self.last_name, self.first_name)
В целях тестирования - я пытаюсь протестировать созданиеa Visit
class VisitModelTest(TestCase):
"""Test class for the Visit model"""
def create_visit(self):
visitor = Visitor.objects.create(type='P', first_name='Test', last_name='McTest', birth_date=date(1981, 10, 16))
student = visitor.student_set.create(first_name='Tester', last_name='McJrTest')
visit = visitor.visit_set.create(visitor=visitor)
visit.students.add(student)
return visit
def test_visit_creation(self):
visit = self.create_visit()
self.visitor = mixer.blend(Visitor, type='P', first_name='Test', last_name='McTest', birth_date=date(1981, 10, 16))
self.students = self.visitor.student_set.create(first_name='Tester', last_name='McJrTest')
self.assertTrue(isinstance(visit, Visit))
self.assertEqual(visit.__str__(), "%s : %s" % (self.visitor, self.students))
В моей строке, где я звоню visit.students.add(student)
, ничего не происходит.
Мои утверждения не с этой ошибкой:
self.assertEqual(visit.__str__(), "%s : %s" % (self.visitor, self.students))
AssertionError: 'McTest, Test : visitor_check_in.Student.None' !=
'McTest, Test : McJrTest, Tester'
- McTest, Test : visitor_check_in.Student.None
+ McTest, Test : McJrTest, Tester
Что такоеподходящий способ добавить студента в этот экземпляр Visit?