unique_together where one of the fields may be NULL

11 views
Skip to first unread message

ALJ

unread,
Mar 12, 2012, 7:50:18 AM3/12/12
to django...@googlegroups.com
I'm struggling to get my models to enforce unique_together. We have the model below where it is possible to have a budget for a whole group (ie there is no break down for a particular sales rep) and therefore the sales_rep field will be left empty. However, it it possible to enter two identical values into the database via the Admin interface where the budget and years are the same while the sales_rep is empty.

I have seen some discussion on it in the archives, but wondered if there is a work around.

Cheers

ALJ

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

class Budget(models.Model):
    budget_type = models.ForeignKey(BudgetType, blank=False)
    sales_rep = models.ForeignKey(SalesRep, null=True, blank=True)
    year = models.IntegerField(blank=False)
    amount = models.FloatField(blank=False)
   
    class Meta:
        unique_together = (("budget_type","sales_rep", "year"),)

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Reply all
Reply to author
Forward
0 new messages