Do not select in case of null value in the database field (Django)

Asked

Viewed 132 times

0

Eae guys, I’m having a problem, I’m making a script that randomly selects database users:

select = User.objects.filter(status=0, premium=0).exclude(twitter_id=select_me.twitter_id).order_by('?').first()

select_me, would be the select of the user who is with the open session, so I don’t need you to select this user on random.

Only gender it stores 1 or 2, integer values, sometimes returns me gender == null, would like to delete the select of those users who have not chosen the genre yet, I have tried:

exclude(twitter_id=select_me.twitter_id, gender=None)

That didn’t work, how can I do that?

1 answer

1


The easiest way to do this is to exclude using the condition isnull.

User.objects.exclude(twitter_id=select_me.twitter_id, gender__isnull=True)

If you also need to delete the empty but not null data from the query, add this other condition:

exclude(gender__exact='')

Here in django documentation has more details that can help you.

  • It worked buddy, gratitude

Browser other questions tagged

You are not signed in. Login or sign up in order to post.