'Django array field filter

In my case customer filed are array filed

customer = ArrayField(models.TextField(),default=[])

my model look like

id customer
1 {1,2,3}
2 {4,2,5}
3 {1,8,9}

i want to filter my model by {5,8} output will be 2th ,3th row



Solution 1:[1]

You can use overlap like this:

customer_ids = MyModel.objects.filter(
     customer__overlap=[5, 8]
).values_list('id', flat=True)
# do something with customer_ids

But using id to determine row number is not right because if you delete an item having an id as 3 and then after that insert a new item then that new item will be given an id of 4. So row number of last row will give you 4th row which is wrong.

You can get row number like this:

row_numbers = [index for index, customer_id in enumerate(customer_ids)]  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1