Opened 5 years ago
Closed 5 years ago
#32078 closed Uncategorized (worksforme)
Inconsistent JSONField filtering for value None
| Reported by: | fre-db | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 3.1 |
| Severity: | Normal | Keywords: | JSONField, None |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When a JSONField has a key with Value None , only filtering on __key__in=[None] works, while you'd expect you could also just filter on __key=None or __key__isnull=True.
Example below:
import django
print(django.__version__)
3.1.2
print(Model.objects.get(pk=99657).jsonfield)
{'key': None}
print(Model.objects.filter(jsonfield__key=None).filter(pk=99657).count())
0
print(Model.objects.filter(jsonfield__key__is_null=True).filter(pk=99657).count())
0
print(Model.objects.filter((jsonfield__key__in=[None]).filter(pk=99657).count())
1
Note:
See TracTickets
for help on using tickets.
It works as expected for me:
>>> NullableJSONModel.objects.create(value={'key': None}) NullableJSONModel object (14) >>> NullableJSONModel.objects.filter(value__key=None) <QuerySet [<NullableJSONModel: NullableJSONModel object (14)>]> >>> NullableJSONModel.objects.filter(value__key__isnull=True) # This will return also JSONFields without the `key`. <QuerySet [<NullableJSONModel: NullableJSONModel object (14)>]> >>> NullableJSONModel.objects.filter(value__key__in=[None]) <QuerySet []>See Storing and querying for None and Key, index, and path transforms docs.