Django ArrayField - a PostgreSQL-specific field

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!

Syntax

  • from django.contrib.postgres.fields import ArrayField
  • class ArrayField(base_field, size=None, **options)
  • FooModel.objects.filter(array_field_name__contains=[objects, to, check])
  • FooModel.objects.filter(array_field_name__contained_by=[objects, to, check])

Remarks

Note that although the size parameter is passed to PostgreSQL, PostgreSQL will not enforce it.

When using ArrayFields one should keep in mind this word of warning from the Postgresql arrays documentation.

Tip: Arrays are not sets; searching for specific array elements can be a sign of database misdesign. Consider using a separate table with a row for each item that would be an array element. This will be easier to search, and is likely to scale better for a large number of elements.



Got any Django Question?