[Answer]-ManyToMany relations and Intermediate models (newbie guidance)

1👍

For many to many fields you should use the plural form. for Person that would be educations and jobs for instance.

Also it is convention to write job_description instead of jobDescription in model fields.

In Person the fields school and company are redundant because that information is accessible through educations (currently education) and jobs (currently work) respectively.

I think you don’t need the many to many field company in Work. A simple ForeignKey should suffice. I think you only have a specific job at ONE company for a given time. You can have mutliple jobs at the same time at multiple companies but still every job is at a single employer. Or am I wrong?

The same goes for Education. You only have a degree in a specific subject from one school even if you went to several schools in the course of this education. Or do you want to model that situation that accurately?

The naming of all dates is a bit misleading since they all are acually years. You also could use NULL in the end year as 'current' and use PositiveIntegerFields.

Now I would have this code:

#both schools and companies
class Institution(models.Model):
    name = models.CharField(max_length = 75)

class Education(models.Model):
    DEGREE_CHOICES = (
                      ('A.A.', 'Associate'),
                      ('Minor', 'Minor'),
                      ('B.A.', 'Bachelor of Arts'),
                      ('B.S.', 'Bachelor of Science'),
                      ('Masters', 'Masters'),
                      ('Ph. D.', 'Doctor of Philosophy'),
                      )
    school = models.ForeignKey(Institution)
    degree = models.CharField(max_length = 5, choices = DEGREE_CHOICES, null = True)
    subject = models.CharField(max_length = 20, null = True)
    start_year = models.PositiveIntegerField()
    end_year = models.PositiveIntegerField(null=True)

class Job(models.Model):
    company = models.ForeignKey(Institution)
    position = models.CharField(max_length = 50, null = True)
    job_description = models.TextField(null = True)
    start_year = models.PositiveIntegerField()
    end_year = models.PositiveIntegerField(null=True)


class Person(models.Model):
    ....
    educations = models.ManyToManyField(Education, blank = True)
    jobs = models.ManyToManyField(Work, blank=True)
    ....

Of course if you want to have choices for the years you can have them

YEAR_CHOICES = ((year, str(year)) for year in range(...))

Alternatively you could write a custom validator for your year fields.

Leave a comment