Feature Request: New PostgreSQL ArrayField

69 views
Skip to first unread message

Liamine Cheridi

unread,
Aug 10, 2022, 12:38:58 PM8/10/22
to django-d...@googlegroups.com

Hello to all,

at Pricemoov we’re using Postgresql arrays (https://www.postgresql.org/docs/15/arrays.html) to store prices of a given product.

More precisely, a given product can have either:

        - standard: N prices (1d array)

        - calendar: D * N prices, when the client is setting up different prices for each day of a given time window (2d array)

So far, we modelled both use cases using a single table queried through raw SQL queries.

We’re currently trying to migrate the raw SQL queries to Django’s ORM, using ArrayField-s.

As per what is written at https://docs.djangoproject.com/en/4.1/ref/contrib/postgres/fields/#arrayfield, in a given field of a table, we can either have:

        - an ArrayField (1d), or

        - an ArrayField(base_field=ArrayField(…) …) (2d)

but cannot have both.

Instead of having different fields or different tables (one for standard and one for calendar use cases), we’re exploring the possibility of adding a custom field ArrayOrMatrixField. To have a better description of that idea, we pushed the PR https://github.com/django/django/pull/15943 (WIP, please do not consider it as a version ready to be reviewed for merge)

Given that such a field is not available in Django yet, we think there may be a reason for that. More precisely, we’d have the following questions:

        - do you anticipate any feature incompatibility/logical bomb that would be introduced by such a custom field?

        - if not, would you be interested in integrating such a field in Django?

Best regards,

Liamine Cheridi, Backend Engineer at Pricemoov


Jacob Rief

unread,
Aug 10, 2022, 12:44:55 PM8/10/22
to Django developers (Contributions to Django itself)
Hello Liamine,
Django offers a JSONField which can be used to store an array.
You can use it to store a list of prices. What's the benefit of using an array instead of JSON?
– Jacob

Adrian Torres

unread,
Aug 11, 2022, 2:12:19 AM8/11/22
to Django developers (Contributions to Django itself)
Hi Liamine,

I think there's various solutions you could try before going the custom field route:

* You could try just using the 2D version and use some sentinel value for the date (NULL?) to treat the case where you just want an array of N prices.
* You could try using django-polymorphic [1] which would allow you to define one "StandardPricingProduct" model and one "CalendarPricingProduct" model, and be able to query them together (e.g. get all products regardless of "type").
* You could try Jacob's suggestion.
* You could try creating a pricing model and have a FK between Pricing and Product -- I know you want to avoid this, but to me it sounds simpler and more maintainable than going the custom field route.

Cheers,
Adrian

Liamine Cheridi

unread,
Aug 11, 2022, 4:06:09 AM8/11/22
to django-d...@googlegroups.com
Hello, thanks a lot for your answers.
I am very embarrassed to say that I went a bit too fast in my testing of nested ArrayField, and it turns out that it actually fits my use case.
   def test_nested_array_accepts_1D_array(self):
    instance = NestedIntegerArrayModel(id=1, field=[1, 2, 3])
    instance.save()
    instance_from_db = NestedIntegerArrayModel.objects.get(id=1)
    self.assertEqual(instance_from_db.field, [1, 2, 3])



This test actually passes, sorry for the noise :S.

Best regards,
Liamine

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/bbbfb68d-1167-45aa-bf8d-4f8b0346c411n%40googlegroups.com.


--

Cordialement,


img

Liamine Cheridi
Backend Engineer
0630178896


imgimgimgimg

Reply all
Reply to author
Forward
0 new messages