Django ArrayField - a PostgreSQL-specific field Querying for membership of ArrayField with contains

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

This query returns all cones with a chocolate scoop and a vanilla scoop.

VANILLA, CHOCOLATE, MINT, STRAWBERRY = 1, 2, 3, 4  # constants for flavors
choco_vanilla_cones = IceCream.objects.filter(scoops__contains=[CHOCOLATE, VANILLA])

Don't forget to import the IceCream model from your models.py file.

Also bear in mind that django will not create an index for ArrayFields. If you are going to search them, you are going to need an index and it will need to be manually created with a call to RunSQL in your migrations file.



Got any Django Question?