Django: allow only one boolean field True

Having such a model, I want to make sure that only one photo for each album has a cover. This will be used as InlineModelAdmin for the album model.

class Photo(models.Model): is_cover = models.BooleanField( default=False) album = models.ForeignKey('Album') image = ImageField(upload_to='uploads') 

How can this be achieved?

+4
source share
1 answer

What photo on the album cover should be for the album property, not for the photo.

Instead of using a boolean property in your photos, use the photo link in your album.

Reply to comment: Assuming that the “built-in model” means that I assume it will be in my comment, you will probably have to provide your own add form template . Using it, you can still provide checkboxes (instead of checkboxes), and then properly configure Album in code that processes the contents of the submitted form.

+5
source

All Articles