Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django queryset filter empty JSONField

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;
like image 731
Othman Avatar asked Jan 18 '26 08:01

Othman


1 Answers

Found a way to achieve using iexact:

MyObject.objects.filter(my_field__iexact="{}")
like image 122
Othman Avatar answered Jan 20 '26 05:01

Othman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!