# NOT query using JSONField but including nulls

**URL:** <https://forum.djangoproject.com/t/not-query-using-jsonfield-but-including-nulls/32288>\
**Category:** Using the ORM\
**Created:** [June 20, 2024, 3:54pm UTC](https://forum.djangoproject.com/t/not-query-using-jsonfield-but-including-nulls/32288 "2024-06-20T15:54:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![djangler](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/djangler/32/18408_2.png) [@djangler](https://forum.djangoproject.com/u/djangler)\
**Post date:** [June 20, 2024, 3:54pm UTC](https://forum.djangoproject.com/t/not-query-using-jsonfield-but-including-nulls/32288/1 "2024-06-20T15:54:10Z")

</div>

I have a `JSONField` (with `default=dict`, not sure if this is relevant in this case) in my model called `data` and I’m using Postgres. If I do `~Q(data__city='Atlantis')` the query excludes not only rows where city is Atlantis, **but also rows where data has no city**.

**Is this expected behavior?** I think so, because the generated query is very simple, so it seems a Postgres behavior.

If so, **what’s the recommended way to also include rows without city?** I did `~(Q(data__city='Atlantis') & Q(data__has_key='city'))`. But it’s a bit verbose. So I would like to know if there is a simpler way.

Thanks for your attention.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [June 20, 2024, 8:50pm UTC](https://forum.djangoproject.com/t/not-query-using-jsonfield-but-including-nulls/32288/2 "2024-06-20T20:50:22Z")

</div>

The [JsonField doc](https://docs.djangoproject.com/en/4.2/topics/db/queries/#key-index-and-path-transforms) covers a few of the scenarios that you’re describing.

---

<div class="post-metadata">

**Author:** ![djangler](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/djangler/32/18408_2.png) [@djangler](https://forum.djangoproject.com/u/djangler)\
**Post date:** [June 21, 2024, 10:10am UTC](https://forum.djangoproject.com/t/not-query-using-jsonfield-but-including-nulls/32288/3 "2024-06-21T10:10:35Z")

</div>

Thanks for your response, Leandro!

As far as I can see this is the most relevant part to my question:

> Due to the way in which key-path queries work, [`exclude()`](https://docs.djangoproject.com/en/4.2/ref/models/querysets/#django.db.models.query.QuerySet.exclude) and [`filter()`](https://docs.djangoproject.com/en/4.2/ref/models/querysets/#django.db.models.query.QuerySet.filter) are not guaranteed to produce exhaustive sets. If you want to include objects that do not have the path, add the `isnull` lookup.

And this seems to answer my first question, that it’s indeed an expected behavior.

To use the isnull as the paragraph suggests would be something like `~Q(data__city='Atlantis') | Q(data__isnull='city')`? But I can’t see a clear advantage in this strategy compared to the has\_key I’m using. In reality, the way I’m building queries in this particular case makes the has\_key strategy much simpler to implement.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [June 21, 2024, 12:41pm UTC](https://forum.djangoproject.com/t/not-query-using-jsonfield-but-including-nulls/32288/4 "2024-06-21T12:41:26Z")

</div>

> [@djangler](#):
>
> To use the isnull as the paragraph suggests would be something like `~Q(data__city='Atlantis') | Q(data__isnull='city')`?

Almost, it would be:

`~Q(data__city='Atlantis') | Q(data__city__isnull=True)`

---

<div class="post-metadata">

**Author:** ![djangler](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/djangler/32/18408_2.png) [@djangler](https://forum.djangoproject.com/u/djangler)\
**Post date:** [June 21, 2024, 1:24pm UTC](https://forum.djangoproject.com/t/not-query-using-jsonfield-but-including-nulls/32288/5 "2024-06-21T13:24:50Z")

</div>

Ops. My confusion, sorry. And thanks!
