""" NOTE: I now have a better implementation of this (nicer api, less signal wrangling) available on PyPI here: https://pypi.python.org/pypi/django-exclusivebooleanfield """ from django.db.models.signals import post_save """ an Exclusive Boolean Field means that only one row in the model table can have that field True at a time... if you supply names in 'with_fields' then only one row in the model table where those additional fields match the instance being saved can have their exclusive boolean fields True at the same time. """ def exclusive_boolean_handler(sender, instance, created, **kwargs): eb_fields = getattr(sender, '_exclusive_boolean_fields', []) with_fields = getattr(sender, '_exclusive_boolean_with_fields', []) uargs = {} for field in eb_fields: ifield = getattr(instance, field) if ifield == True: uargs.update({field:False}) fargs = {} for field in with_fields: ifield = getattr(instance, field) fargs.update({field:ifield}) sender.objects.filter(**fargs).exclude(pk=instance.pk).update(**uargs) def exclusive_boolean_fields(model, eb_fields=[], with_fields=[]): setattr(model, '_exclusive_boolean_fields', eb_fields) setattr(model, '_exclusive_boolean_with_fields', with_fields) post_save.connect(exclusive_boolean_handler, sender=model) """ Example usage: """ class Address(models.Model): account = models.ForeignKey(Account, related_name='addresses') default = models.BooleanField(default=False) country = CountryField() postcode = models.CharField(max_length=16) line1 = models.CharField(max_length=128) line2 = models.CharField(max_length=128, blank=True, null=True) line3 = models.CharField(max_length=128, blank=True, null=True) exclusive_boolean_fields(Address, ('default',), ('account',)) """ i.e. if you set the 'default' field to True on an instance, then it will be set False on all the other rows with the same 'account' value """