I’d really appreciate some guidance on how to construct the following models. I want to make sure I’m doing this in the ideal way.
I want to keep track of some info about employees at my work. Basically, past Education and past Work Experience
So here’s the relations I can think of
- Education
- An employee could been to several school and each school can have many students (m2m)
- Students attend a school for a period of time
- a student can have multiple degrees form the same school and that school offers multiple degrees (m2m)
- Work (almost the same as education)
- m2m relation with employees and companies
- employees work at a company to a set time
- employes could have several jobs at one company (m2m)
Following basically what’s above, here’s the code I’ve worked out:
#make a list of numbers in a tuple
START_DATE_CHOICES = dict((str(x),str(x)) for x in range(date.today().year-30,date.today().year+1))
END_DATE_CHOICES = START_DATE_CHOICES
START_DATE_CHOICES = START_DATE_CHOICES.items()
START_DATE_CHOICES.sort()
START_DATE_CHOICES.reverse()
START_DATE_CHOICES = tuple(START_DATE_CHOICES)
END_DATE_CHOICES['current'] = 'current'
END_DATE_CHOICES = END_DATE_CHOICES.items()
END_DATE_CHOICES.sort()
END_DATE_CHOICES.reverse()
END_DATE_CHOICES = tuple(END_DATE_CHOICES)
#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.ManyToManyField(Institution, through='Dates')
degree = models.CharField(max_length = 5, choices = DEGREE_CHOICES, null = True)
subject = models.CharField(max_length = 20, null = True)
class Work(models.Model):
company = models.ManyToManyField(Institution, through='Dates')
position = models.CharField(max_length = 50, null = True)
jobDescription = models.TextField(null = True)
class Dates(models.Model):
education = models.ForeignKey(Education, null = True)
work = models.ForeignKey(Work, null = True)
institution = models.ForeignKey(Institution)
start = models.CharField(max_length = 4, choices = START_DATE_CHOICES)
end = models.CharField(max_length = 7, choices = END_DATE_CHOICES)
class Person(models.Model):
....
school = models.ManyToManyField(Institution, blank=True)
education = models.ManyToManyField(Education, blank = True)
company = models.ManyToManyField(Institution, blank = True, related_name="%(app_label)s_%(class)s_related")
work = models.ManyToManyField(Work, blank=True)
....
So my question is: Is this an acceptable way of doing it? I’m a newbie and I’m not sure if I’m just abusing relationships
also, I’m sure there’s a easier way to do the whole start/end date thing… wasn’t able to figure it out though
Any pointers would be greatly appreciated, Thank you!
For many to many fields you should use the plural form. for
Personthat would beeducationsandjobsfor instance.Also it is convention to write
job_descriptioninstead ofjobDescriptionin model fields.In
Personthe fieldsschoolandcompanyare redundant because that information is accessible througheducations(currentlyeducation) andjobs(currentlywork) respectively.I think you don’t need the many to many field
companyinWork. A simpleForeignKeyshould 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
NULLin the end year as'current'and usePositiveIntegerFields.Now I would have this code:
Of course if you want to have choices for the years you can have them
Alternatively you could write a custom validator for your year fields.