Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Allowing only one booleanfield to be True

Tags:

python

django

Having a such model, I want to make sure only one Photo for each album to have a cover. This will be used as InlineModelAdmin to 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 ?

like image 293
Hellnar Avatar asked Aug 12 '11 20:08

Hellnar


1 Answers

Which photo is on the cover of the album should be a property of the album, not the Photo.

Instead of using a boolean property in your Photos, use a reference to a Photo in your Album.

Answer to Comment: Assuming that 'inline model' means what I assume it to be in my comment, you will probably have to provide your own add form template. Using it, you can still provide the option boxes (instead of check boxes) and then set up the Album correctly in the code that processes the contents of the submitted form.

like image 77
blubb Avatar answered Sep 24 '22 15:09

blubb