views:

97

answers:

1

I am new to django (and have no experience in SQL), so maybe there is no standard way of doing this.

So assume I use the band,person,membership example from http://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-intermediary-models

What if I want to make sure, there is at max one membership bertween a fixed person and a fixed band. A person can have only one "invite_reason" and "join_date" for the band. Is there some kind of "unique" flag like for onetomany models.

I guess i can write a routine to check and erase any new ones. Where would one such routine live and what is going to call it (cron, django signals (which i don't understand yet))?

+2  A: 

I may have misunderstood your question, but maybe you want to look into the inner Meta class?

class Membership(models.Model):

    class Meta:
        unique_together = (('person'), ('group'), )

    person = models.ForeignKey(Person)
    group = models.ForeignKey(Group)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)

This would add the constraint that a person can never be a member of a group twice. In technical terms, there may never exist two rows in this table where "person" and "group" have the same value.

If you want to make sure that only e.g. person with PK 7 should never be a member of group with PK 11 twice, you should really look into Django signals as you said, specifically the pre_save hook.

Deniz Dogan