Django ManyToMany Relationship with Extra Fields

I want to save some additional information in this automatically created ManyToMany join-table. How do I do this in Django?

In my case, I have two tables: "Employees" and "Projects". I want to store how much each employee receives per hour of work in each of the projects, because these values ​​do not match. So how would I do that?

What occurred to me, instead of the "ManyToManyField" method, was to create an explicitly third class / table to store this new information and establish its connection with the "Employees" and "Projects" using the "ForeignKey" method. I'm sure this will work, but is this the best approach?

+44
database django many-to-many model
Dec 14 '10 at 19:15
source share
1 answer

Here is an example of what you want to achieve:

http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

In case of link break:

from django.db import models class Person(models.Model): name = models.CharField(max_length=128) def __str__(self): # __unicode__ on Python 2 return self.name class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Membership') def __str__(self): # __unicode__ on Python 2 return self.name class Membership(models.Model): person = models.ForeignKey(Person) group = models.ForeignKey(Group) date_joined = models.DateField() invite_reason = models.CharField(max_length=64) 
+65
Dec 14 '10 at 7:19
source share



All Articles