V
V
Vasily Vorobyov2016-02-17 07:32:38
Django
Vasily Vorobyov, 2016-02-17 07:32:38

How to allow only one True value for a BooleanField ForeignKey to be accepted?

There is this model:

class ProductImage(models.Model):
    product = models.ForeignKey(_(u"Продукт"), Product, related_name='images')
    image = models.ImageField(_(u"Изображение"), upload_to="product_images/")
    is_main = models.BooleanField()

How can I make sure that among all ProductImages that refer to Product, there can only be 0 or 1 can take the value is_main=True?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question