A
A
Alexey Belov2018-11-28 00:00:46
Django
Alexey Belov, 2018-11-28 00:00:46

How to make unique boolean among all user instances?

I have several forms, one edit, the second crate, the user has several instances, they have a default boolean, how to make sure that only one default=True remains when saving, all user instances
I remember there was some kind of mixin, or overriding the save , I can not find

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2018-11-28
@Alenorze

if self.the_only:
    type(self).objects.exclude(pk=self.pk).filter(the_only=True).update(the_only=False)

Don't forget to do this only on the_only changes. Plus, it's not clear how to deal with the removal of the only "marked" one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question