Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map PostgreSQL array field in Django ORM

I have an array field in my PostrgreSQL database of type text. Is there a way to map this into a Django model ?

like image 509
Nicu Surdu Avatar asked Dec 09 '10 16:12

Nicu Surdu


4 Answers

You might want to look into django-dbarray on github. It adds support for postgresql array fields.

I haven't used it before, but it looks like you just need to do:

from django.db import model
import dbarray

class ProfilingTestRun(models.Model):
    function = models.CharField(max_length=64)
    runtimes = dbarray.FloatArrayField()
like image 60
jcdyer Avatar answered Nov 09 '22 23:11

jcdyer


One of the other nice options is http://django-orm.readthedocs.org/ --- a library that adds bindings to many native postgres types.

Main drawback of django-orm is that as of today it has no working support for south.

like image 4
jb. Avatar answered Nov 09 '22 23:11

jb.


djorm-ext-pgarray also offer queries http://www.niwi.be/2012/10/07/postgresql-array-fields-with-django/

like image 4
ecabuk Avatar answered Nov 10 '22 00:11

ecabuk


Native support for PostgreSQL specific model fields is coming soon to Django (in the django.contrib.postgres.fields module):

  • https://docs.djangoproject.com/en/dev/ref/contrib/postgres/fields/#arrayfield
  • https://github.com/django/django/pull/2485 : The corresponding pull request
like image 3
user Avatar answered Nov 10 '22 00:11

user