【发布时间】:2018-03-12 03:19:21
【问题描述】:
我有一个模型模型。该模型有多个属性,其中三个是:domaintld、subdomain、url - OneToOneFields。
我试图让这些字段中的一个且只有一个不为空。
我的方法有效,但我很好奇是否有更好的方法来做到这一点(我使用 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")
【问题讨论】:
标签: django postgresql django-models model