如何在 Django ORM 中映射 PostgreSQL 数组字段

发布于 2024-10-06 20:48:28 字数 156 浏览 0 评论 0 原文

我的 PostrgreSQL 数据库中有一个文本类型的 array 字段。有没有办法将其映射到 Django 模型中?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

假装爱人 2024-10-13 20:48:28

您可能想查看 github 上的 django-dbarray 。它添加了对 postgresql 数组字段的支持。

我以前没有使用过它,但看起来你只需要这样做:

from django.db import model
import dbarray

class ProfilingTestRun(models.Model):
    function = models.CharField(max_length=64)
    runtimes = dbarray.FloatArrayField()

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()
2024-10-13 20:48:28

其他不错的选择之一是 http://django-orm.readthedocs.org/ ---一个向许多本机 postgres 类型添加绑定的库。

django-orm 的主要缺点是截至目前它还没有对 south 的有效支持。

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.

情独悲 2024-10-13 20:48:28

Django 即将对 PostgreSQL 特定模型字段提供本机支持(在 django.contrib.postgres.fields 模块中):

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

静若繁花 2024-10-13 20:48:28

Django 1.8 开始,有一个 django.contrib.postgress 模块 添加了对 数组字段以及其他 PostgreSQL 数据类型。

例如你可以这样做:

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)

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)
半透明的墙 2024-10-13 20:48:28

您必须子类化 model.Field 并编写输入和输出方法。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文