我的 Django 应用程序中有这个模型:
class ClubSession(models.Model):
location = models.CharField(max_length=200)
coach = models.ForeignKey('auth.User', on_delete=models.CASCADE)
date = models.DateTimeField(default=now)
details = models.TextField()
def __str__(self):
return self.location
这个视图实现了它:
class SessionListView(ListView):
model = ClubSession
template_name = 'club_sessions.html'
context_object_name = 'all_club_sessions_list'
我正在尝试测试视图。我的测试类有一个setUp创建记录的:
def setUp(self):
ClubSession.objects.create(location='test location',
coach=User(id=1),
date='2020-06-01 18:30',
details='this is another test')
当我运行我的测试时,我得到这个错误:
IntegrityError: The row in table 'club_sessions_clubsession' with primary key '1' has an invalid foreign key: club_sessions_clubsession.coach_id contains a value '1' that does not have a corresponding value in auth_user.id.
存在一个 ID 为 1 的用户,那么我该如何让它工作呢?我试过添加用户名,但也没有用。
慕村225694
相关分类