Filter by length of ArrayField error

For the life of me I cannot figure this out in Django 3.2, Python 3.8 and Postgres. I am simply trying to filter an ArrayField by length like so:

User.objects.annotate(test=models.Value([],ArrayField(models.IntegerField()))).filter(test__len=0)

I get the error message

IndexError: tuple index out of range

As far as I can tell, I am following the docs exactly. What gives?

p.s. the model I use is irrelevant. No matter the model I annotate I still get the same error.

Well I’ve found this SO answer which I can try. I’m still a bit confused why what I’m doing won’t work, and if my example won’t work then what is the proper use case for __len on ArrayFields?

UPDATE: The strategy of using the Postgres CARDINALITY function does seem to work for me either. I get the error TypeError: 'int' object is not iterable.

The issue you are encountering started manifesting itself in Django 1.10 due to a NULL handling corrective and no one reported the issue upstream until Django 4.1 where it was fixed pretty quickly.

Django 3.2 is EOL since April 1st and doesn’t include the corrective, upgrading to Django 4.2 should address your problem.

Great reason to upgrade. Thanks!