I have a model which has a boolean field:
class Library(SocialLinksModelMixin):
"""
Represents book shelves and places where people have / leave books.
"""
user = models.ForeignKey(...)
is_main = models.BooleanField(default=False)
User can have multiple Libraries and I would like to allow setting the main one to ease the flow in the app.
What is the easiest way for switching only one Library is_main to True and all others to False (There is always only 1 Library for a user that is main)
I'm thinking about querying for all libraries and updating each and every one. Maybe there is another way to do that functionality?
If you have the primary key of the
Library
to set theis_main
toTrue
, you can work with:You might also want to enforce at database level that there can only be one
Library
withis_main=True
peruser
: