Toms River
Toms River

Reputation: 366

Working with Boolean QuerySet

I'm having troubles working with a Boolean QuerySet.

models.py:

 class Profile(models.Model):
     user = models.OneToOneField(User, related_name='profile', primary_key=True)
     firstConnexion = models.BooleanField(default=True)

views.py

 firstConnexion = Profile.objects.filter(user=request.user).values('firstConnexion')

 if firstConnexion:
     Profile.objects.filter(user_id=user_id.id).update(firstConnexion=False)
     return redirect('/one/')
 else:
     return redirect('/two/')

The problem is I am only getting the first condition even though Profile is updated to False

How ?

Upvotes: 0

Views: 34

Answers (1)

Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13723

The reason is that .values() that you used returns a list of boolean values. In your case, it will return this

[False]

So, the following will evaluate to True:

if [False]

as there is something inside the list.

Try the following:

if request.user.profile.firstConnexion:
    ...

Hope it helps!

Upvotes: 1

Related Questions