How to map PostgreSQL array field in Django ORM

一个人想着一个人 提交于 2019-11-27 03:52:49

问题


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


回答1:


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()



回答2:


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.




回答3:


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




回答4:


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



回答5:


Since Django 1.8 there is a django.contrib.postgress module that adds support to array fields among other PostgreSQL data types.

For example you can do something like this:

from django.contrib.postgres.fields import ArrayField
from django.db import models

class GoGame(models.Model):
    board = ArrayField(ArrayField(models.IntegerField(),size=19),size=19)



回答6:


you have to subclass model.Field and write input and output methods.

http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#custom-database-types



来源:https://stackoverflow.com/questions/4400762/how-to-map-postgresql-array-field-in-django-orm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!