I have a task:
- To make a database model with essentially ‘student’ and ‘group’.
- ‘Student’ contains: ‘Name’, ‘student ID card’, ‘group’ (must be ForeigKey to Group!)
- ‘Group’ contains: ‘group name’ and ‘captain’ (must be foreign key to Student!)
Now I have a collision – right straight it’s impossible. So I did it in this way:
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)
Do I have more elegant and correct way to do this?
Try to add a related_name: