How to unite together 2 model fields in Django?

时间:2016-02-12 20:04:26

标签: python django django-models metaclass

I have 2 fields in my model class

class A(model.Model):
    field1 = models.ImageField(upload_to='path', null=True)
    field2 = models.URLField(null=True)

I need to unite so that if first field is full, second field can't be filled. And vice versa.

I try to create Meta class in class A with field unique_together:

class Meta:
    unique_together = (field1, field2)

but in that case, both field can't be empty and both can be full. But I need that just 1 of fields must be full.

1 个答案:

答案 0 :(得分:1)

Customize save() method in you A model:

EnableQuery