3

I would like to filter a JSONField my_fieldwhere the JSon is not empty. In the documentation there are has_keyand has_any_key methods but I don't know the possible keys in advance. In SQL it should be something like:

select * from my_model where cardinality(my_field)>0;
Othman
  • 332
  • 5
  • 19

2 Answers2

4

Found a way to achieve using iexact:

MyObject.objects.filter(my_field__iexact="{}")
Othman
  • 332
  • 5
  • 19
0

according to the docs you can use

MyModel.objects.include(myjsonfield__exact=[])
MyModel.objects.exclude(myjsonfield__exact=[])
pymen
  • 5,737
  • 44
  • 35