[Fixed]-Django Boolean Queryset Filter Not Working

17👍

From what you’ve posted, everything is working as advertised. If you try this stuff from the shell, you should get the following results. Of course I’m making some of it up, so read before you just copy-paste.

>>> from myapp.models import Property
>>> Property.objects.all()
[<Property: Property object>,<Property: Property object>,<Property: Property object>,<Property: Property object>,<Property: Property object>,]
>>> Property.objects.filter(sold=False)
[]
>>> Property.objects.filter(sold=True)
[<Property: Property object>,<Property: Property object>,<Property: Property object>,<Property: Property object>,<Property: Property object>,]
>>> Property.objects.create(sold=False, my='other', fields=1)
>>> Property.objects.filter(sold=False)
[<Property: Property object>,]

Jack is right, 1 should evaluate to True in most SQL implementations.

23👍

This has happened to me as well.

Turned out in SQLite you can have Boolean with value 0 and Boolean with value False.

So Django does not work with the ones set to False.

I saw this discrepancy in SQLiteMan.

Simple update fixed the problem.

I think this happened during schema upgrades and migration in my dev environment, so I am not too worried about it.

👤Kiril

2👍

I had the same problem. My solution was to change the type of the column from a ‘bit’ to a ‘tinyint’.

The issue in my case was caused by a manually added column in a table.

Leave a comment