I have a model Model. This model have multiple attributes, three of them are: domaintld
, subdomain
, url
- OneToOneField
s.
I'm trying to allow one and only one from these fields to be non empty.
My approach works but I'm curious if it's there a better way to do this (I use PostgreSQL).
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
self.clean()
super(Model, self).save(force_insert, force_update, using, update_fields)
def clean(self):
super(Model, self).clean()
if not(((bool(self.url) ^ bool(self.domaintld)) ^ bool(self.subdomain)) and not(self.url and self.domaintld and self.subdomain)):
raise exceptions.ValidationError("One and only one field can be used: url,domaintld,subdomain")