11π
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.
31π
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
Everything you could do with list, you can do with ArrayField. Even sorting
Django saves ArrayField
as python list
These means that it saves structure and elements of python list.
- [Django]-Factory-boy create a list of SubFactory for a Factory
- [Django]-"No 'Access-Control-Allow-Origin' header is present on the requested resource" in django
- [Django]-Using Django auth UserAdmin for a custom user model
11π
This works:
from django.db.models import F
from django.db.models.expressions import CombinedExpression, Value
post = 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'])))
- [Django]-Django REST Framework ModelSerializer get_or_create functionality
- [Django]-How can I fill up form with model object data?
- [Django]-Can you change a field label in the Django Admin application?
5π
Another solution is using a custom expression. I tested the following code with Django 1.11 and Python 3.6 (f-strings).
from django.db.models.expressions import Func
class ArrayAppend(Func):
function = 'array_append'
template = "%(function)s(%(expressions)s, %(element)s)"
arity = 1
def __init__(self, expression: str, element, **extra):
if not isinstance(element, (str, int)):
raise TypeError(
f'Type of "{element}" must be int or str, '
f'not "{type(element).__name__}".'
)
super().__init__(
expression,
element=isinstance(element, int) and element or f"'{element}'",
**extra,
)
The expression can be used in update()
:
Post.objects \
.filter(pk=1) \
.update(tags=ArrayAppend('tags', 'new tag'))
- [Django]-Django rest framework create nested objects "Models" by POST
- [Django]-How to handle per object permission in Django nowadays?
- [Django]-Send_mass_emails with EmailMultiAlternatives
2π
You could use django_postgres_extensions. It supports a lot of functions like append, prepend, remove, concatenate.
But if you are using Django 1.8 like me, you should use only the required classes from this package. That way, you wonβt have to change database backend too. Iβve pasted the required classes here. Use them as described in first link.
- [Django]-Django annotation with nested filter
- [Django]-Django TemplateSyntaxError Could not parse the remainder: '()'
- [Django]-Sharing django sessions on specific subdomains
2π
This 100% works
from django.db.models import F
from django.db.models.expressions import CombinedExpression, Value
Post.objects.filter(created_on__lt=now() - timespan(days=30)).update(tags=CombinedExpression(F('tags'), '||', Value('{hello}')))
Note the difference between my variant and Yotam Ofekβs:
Value(["hello"]) should be changed to Value("{hello}")
Also if you need to exclude entries with some specific tag use this:
.exclude(tags__contains="{hello}")
One more moment, field "tags" should have default=list
tags = ArrayField(models.CharField(max_length=200), default=list)
- [Django]-Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
- [Django]-In-Memory broker for celery unit tests
- [Django]-How Can You Create an Admin User with Factory_Boy?