[Fixed]-Exclude null values from Django's ArrayAgg

8👍

I’ve tried your syntax Q(field__isnull=False) and also ~Q(field=None); both work fine for me on Django 2.1.7 and PG 11.2: I get [] instead of [None].

In the Django shell, you can check the SQL query that Django generates for your queryset:

print(str(your_queryset.query))

The relevant SQL portion in my test was:

ARRAY_AGG("table"."field_id") 
    FILTER (WHERE "table"."field_id" IS NOT NULL)
    AS "agg"

Depending on the syntax variant used, you can also get the following, which however works out the same:

FILTER (WHERE NOT ("table"."field_id" IS NULL))

Leave a comment