On 26 sep, 12:02, Fabio Natali <[EMAIL PROTECTED]> wrote: > Hi everybody. > > my django website has to deal with a set of workers, each of them > earning a given amount of money for their own work. > > So I have: > > class Worker(models.Model): > name = models.CharField(max_length=30) > hourly_wage = models.DecimalField(max_digits=10, decimal_places=2) > > A part from their own work, those workers work together in groups and > receive additional gains for those extra team work. The more a worker > has been involved in a certain team, the more money he/she gets. Each > worker can take part to any number of teams. > > The problem is: how can I use a model to depict this scenario? Model > "Team" should be a subset of the set of workers, each of them provided > with a percentage (= the involvment of worker W within team T). > > If Alice, Bob, Carl, David, Emmanuel, Frank are workers, then a team > could be: > > Team0 = {(Alice, 60%), (David, 30%), (Frank, 10%)}
Assuming you're using Django >= 1.0 (if you don't, you'll have to drop the 'through' argument for ManyToManyField and probably add custom helper methods and/or a custom Manager): class Team(models.Model): name = models.CharField(max_length=128) # http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany workers = models.ManyToManyField( Worker, through='TeamMembership' ) class TeamMembership(models.Model): team = models.ForeignKey(Team) worker = models.ForeignKey(Worker) involvement = models.FloatField() class Meta: unique_together = (('team', 'worker'),) Not tested of course, but this should get you started.... Next point is probably to ensure that the sum of TeamMembership.involvements for a given team is not > 1.0. I think you'll have no other solution than overriding the save method of TeamMembership. My 2 cents.... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---