Django 1.8 ArrayField append&延伸 [英] Django 1.8 ArrayField append & extend

查看:141
本文介绍了Django 1.8 ArrayField append&延伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 1.8 将附带新的高级字段类型,包括ArrayField ,这些依赖于PostgreSQL并在DB级别实现。



PostgreSQL的数组字段实现了一个追加方法



然而,我找不到有关将项目附加到 ArrayField的任何文档。这显然是非常有用的,因为它将允许更新该字段,而不将其全部内容从数据库中传输回来。



这可能吗?如果没有,将来会有可能吗?任何指向我错过的文档的指针都将不胜感激。



为了澄清我的问题,这将是非常好的:



注意:这是幻想代码,我假设不会工作(我没有尝试过)



<$ p $
class Post(models.Model):
tags = ArrayField(models.CharField(max_length = 200))

#其他地方:
p = Post.objects.create(tags = [str(i)for i in range(10000)])
p.tags.append('hello')

目前是否有任何方法可以不使用原始的sql?

解决方案

我认为您正在寻找的功能目前尚未实施(可能不会计划)。许多Postgres contrib功能起源于此kickstarter项目



我发现新功能最有用的文档来自源代码本身。其中包括指向许多这些功能的原始拉取请求的链接。



关于提到的Array函数的一个重要注意事项,它们是函数,并且可以说是超出典型ORM的范围



我希望这些信息是有用的,你会发现一个很好的解决方案。 >

Django 1.8 will come with new advanced field types including ArrayField these rely on PostgreSQL and are implemented at a DB level.

PostgreSQL's array field implements an append method.

However I can't find any documentation on appending an item to an ArrayField. This would clearly be very useful as it would allow the field to be updated without transferring its entire contents from the db then back to it.

Is this possible? If not will it be possible in future? Any pointers to docs I've missed would be much appreciated.

To clarify what I'm asking about, this would be great:

Note: this is fantasy code and I assume won't work (I haven't tried)

# on model:
class Post(models.Model):
    tags = ArrayField(models.CharField(max_length=200))

# somewhere else:
p = Post.objects.create(tags=[str(i) for i in range(10000)])
p.tags.append('hello')

Is there currently any way of doing this without resorting to raw sql?

解决方案

I think the features you are looking for are currently not implemented (and may not be planned). Many of the Postgres contrib features originated based on this kickstarter project.

I find the most useful documentation for the new features come from the source code itself. Which includes a link to the original pull request for many of these features.

An important note in regards to the Array Functions mentioned, they are Functions and arguably outside the scope of a typical ORM.

I hope this information is useful and you find a nice solution to this issue.

这篇关于Django 1.8 ArrayField append&amp;延伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆