Thursday, November 21, 2013

Re: Model Design: Adding Params to Relationship FIelds

On Wednesday, 20 November 2013 22:49:28 UTC, Thomas Murphy wrote:
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()])


This is well covered in the documentation under "Extra fields on many-to-many relationships":
https://docs.djangoproject.com/en/1.6/topics/db/models/#intermediary-manytomany 
--
DR.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

No comments:

Post a Comment