我有一个任务:
现在我发生了碰撞 - 直接说不可能。所以我这样做了:
class Group(models.Model):
group_name = models.CharField(max_length=50)
class Student(models.Model):
name = models.CharField(max_length=50)
birth_date = models.DateField()
std_ID_card = models.IntegerField()
group = models.ForeignKey(Group)
class Captain(models.Model):
student = models.OneToOneField(Student)
group = models.OneToOneField(Group)
我有更优雅和正确的方法吗?
答案 0 :(得分:2)
尝试添加related_name:
class Student(models.Model):
name = models.CharField(max_length=50)
birth_date = models.DateField()
std_ID_card = models.IntegerField()
group = models.ForeignKey('Group')
class Group(models.Model):
group_name = models.CharField(max_length=50)
captain = models.ForeignKey('Student', related_name='%(class)s_captain')
答案 1 :(得分:1)
为什么原始结构不可能?
class Group(models.Model):
group_name = models.CharField(max_length=50)
captain = models.ForeignKey('Student')
class Student(models.Model):
name = models.CharField(max_length=50)
birth_date = models.DateField()
std_ID_card = models.IntegerField()
group = models.ForeignKey(Group)