Model Design: Adding Params to Relationship FIelds

15 views
Skip to first unread message

Thomas Murphy

unread,
Nov 20, 2013, 5:49:28 PM11/20/13
to django...@googlegroups.com
Hi all,

I'm working on a hour-reporting system.

Each Project has many Users, and each User has many Projects(These are
established and working nicely).

My design issue is this:
How can I assign each User an IntegerField "Hours" on each Project
they are assigned to? This Integer Field is personal to them, but must
be able to summed by the Project "Hours" count later(This last part is
trivial to design, just wanted to include)

Here's relevant code:

class Project(models.Model):
client = models.CharField(max_length=500)
name = models.CharField(max_length=500)
campaign_start_date = models.DateField()
hours = models.IntegerField(default=0)
members = models.ManyToManyField(User)
hours = models.IntegerField(default=0)

def __unicode__(self):
return self.name

class UserProfile(models.Model):

user = models.OneToOneField(User)

#Additional Attributes Defined Below
project = models.ManyToManyField(Project)
rate = models.IntegerField(default=0)

def __unicode__(self):
return self.user.username

def project_names(self):
return ', '.join([p.name for p in self.project.all()])

Daniel Roseman

unread,
Nov 21, 2013, 5:32:04 AM11/21/13
to django...@googlegroups.com
This is well covered in the documentation under "Extra fields on many-to-many relationships":
--
DR.

Thomas Murphy

unread,
Nov 21, 2013, 8:27:28 AM11/21/13
to django...@googlegroups.com
Thanks Daniel!

I try to always RTFM, but this slipped by me.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/6e6d5b33-ce10-4dac-b2e1-a7f64c8ada7e%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages