Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a Django arrayfield has data

Tags:

django

I have a Django model for articles with an array field that sometimes has ids.

id_list = ArrayField(models.CharField(max_length=10000), blank=True, null=True)

I want to write a query that finds all the article objects that have data in the id_list. I tried the following, but it did not work.

Article.objects.filter(id_list__isnull=False)

What is the correct way to write this? Thank you.

like image 632
henrich Avatar asked Apr 01 '20 02:04

henrich


Video Answer


1 Answers

You can use the len filter on an ArrayField and filter where the length of the array is greater than 0

Article.objects.filter(id_list__len__gt=0)
like image 106
Iain Shelvington Avatar answered Oct 16 '22 17:10

Iain Shelvington