The one-to-many relationship you are looking for does not exist in Django. This is not necessary, since you can do it the other way around: with the attitude of ForeignKey from qualification to user. According to your example, you define models as follows:
class Qualification(models.Model): qualification = models.CharField(max_length=250) max_marks = models.IntegerField(max_length=50) marks_obtained = models.IntegerField(max_length=50) qualifying_year = models.DateField(auto_now=False, null=True) user = models.ForeignKey('CompanyUser', related_name='qualifications') class CompanyUser(User): date_of_birth = models.DateField(auto_now=False, null=True) position = models.CharField(max_length=100)
So there is no relation specified from CompanyUser to Qualification, but you have a ForeignKey relation from Qualification to CompanyUser (i.e. each qualification belongs to one user, but several qualifications may belong to the same user). Django automatically supports feedback from CompanyUser to Qualification, which is the one-to-many relationship you are looking for. related_name option for ForeignKey, you can specify a meaningful name for the reverse relationship (in this case, "qualification"). Now you can simply access the Qualifications of the CompanyUser object with the user.qualifications attribute.
In admin, you first create a new CompanyUser, and then an arbitrary number of qualifications that you assign to this user through ForeignKey.
Be sure to read about this in the dango documentation section on model relationships .
j0ker
source share