Django 1.8 ArrayField append & extend Django 1.8 ArrayField append & extend python python

Django 1.8 ArrayField append & extend


Note: OP code will absolutely work. We just need to save the model (because these is just a model field, not relation). Let's see:

>>> p = Post.objects.create(tags=[str(i) for i in range(10000)])>>> p.tags.append("working!")>>> p.save()>>> working_post = Post.objects.get(tags__contains=["working!"])<Post: Post object>>>> working_post.tags[-2:][u'9999', u'working!']

Going deeper

Django gets ArrayField as python list

Code reference

Everything you could do with list, you can do with ArrayField. Even sorting

Django saves ArrayField as python list

Code reference

These means that it saves structure and elements of python list.


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.


This works:

from django.db.models import Ffrom django.db.models.expressions import CombinedExpression, Valuepost = Post.objects.get(id=1000)post.tags = CombinedExpression(F('tags'), '||', Value(['hello']))post.save()

or in an update clause:

Post.objects.filter(created_on__lt=now() - timespan(days=30))\    .update(tags=CombinedExpression(F('tags'), '||', Value(['old'])))