Django, JSONField, filter for non empty list

Viewed 718

I have a jsonfield

foo = JSONField(default=list)

I want to filter queryset so that foo has some data (not empty list)

I've tried

MyModel.objects.filter(foo__ne=[])  # doesn't seem to work
MyModel.objects.filter(foo__gt=[])  # seems to work but can't be sure if it's the right approach
2 Answers

You should be able to test inequality by negating equality using Q objects.

from django.db.models import Q

MyModel.objects.filter(~Q(foo=[]))
Related