Sandeep Gupta
Sandeep Gupta

Reputation: 55

Can a model referred by a foreign key have its own foreign key to refer some other model?

I want to make a model of school under which using foreignkey I can refer to all the students.. now the students could have there medical history and counselling history.. I want medical model and councelling model to be reffered by students Id.

The plan is to make a multi school model where students can be reffered by there school name but all the students have there own medical and counselling history.

Please explain how to achieve this in django by python 3.

Upvotes: 0

Views: 46

Answers (1)

Mohamed ElKalioby
Mohamed ElKalioby

Reputation: 2334

class School(models.Model):
    name = models.CharField(max_length=255)
    .......
  
  class Student(models.Model):
     first_name = models.CharField(max_length=255)
     last_name = models.CharField(max_length=255)
     ...........
     school = models.ForeignKey(School, on_delete=models.setNull)

  class MedicalHistory(models.Model):
     student = models.ForeignKey(Student, on_delete=models.cascade)
     .................

and the same for the counseling Model

Upvotes: 1

Related Questions