Should I use an end-to-end model with respect to M2M

I am adding user education to its userprofile. A user may have several entries for his training. Should I use basic M2M relationships such as -

class Education(models.Model): school = models.CharField(max_length=100) class_year = models.IntegerField(max_length=4, blank=True, null=True) degree = models.CharField(max_length=100, blank=True, null=True) class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) educations = models.ManyToManyField(Education) 

Or should I use an end-to-end model for this relationship? Thanks.

+4
source share
2 answers

@manji : Django will create a mapping table whether you use through .

To provide an example of why you can add additional fields to the broker or through table:
You may have a field in the through table to track whether this particular education represented the final school in which the person participated:

 class Education(models.Model): ... class UserProfile(models.Model): ... educations = models.ManyToManyField(Education, through='EduUsrRelation') class EducationUserRelation(models.Model): education = models.ForeignKey(Education) user_profile = models.ForeignKey(UserProfile) is_last_school_attended = models.BooleanField() 
+2
source

Django will automatically create an intermediary to represent the ManyToMany relationship between the two models.

If you want to add more fields to this table, specify your own table (for example, model) using the through attribute, otherwise you do not need to.

+2
source

All Articles