Django Bulk Update QuerySet objects
Using bulk_update to update multiple objects in one go.#django #python
Let's say, I have a lots of objects which I want to update with a particular field or fields. We can use the bulk_update method with the model name.
# blog/models.py from django.db import models ARTICLE_STATUS = [ ("PUBLISHED", "Published"), ("DRAFT", "Draft"), ] class Article(models.Model): title = models.CharField(max_length=128) description = models.CharField(max_length=512) content = models.TextField() status = models.CharField(max_length=16, choices=ARTICLE_STATUS, default="DRAFT") def __str__(self): return self.title
Let's say we have a simple model
with a few typical attributes like
. We have the status as a choice field from two options as
. It could be a boolean field, but that looks too gross for a article status.
from blog.models import Article articles = Article.objects.filter(status="draft") for i in range(len(articles)): articles[i].status = "published" Article.objects.bulk_update(articles, ["status",])
In the above code, the
model is filtered by the status of
. We iterate over the
which will contain the objects of the articles, by setting the object's properties to the value we want to set. We are jsut setting the value of the property of the object for each object.
This just makes a changes to the
, by using the
method, the two parameters required are the
and the list of
which are to be updated. The function returns the number of objects/records updated.
>>> from blog.models import Article >>> articles = Article.objects.filter(status="DRAFT") >>> articles <QuerySet [<Article: test 1>, <Article: test 3>]> >>> for i in range(len(articles)): ... articles[i].status = "PUBLISHED" ... >>> articles <QuerySet [<Article: test 1>, <Article: test 3>]> >>> >>> Article.objects.bulk_update(articles, ['status',]) 2 >>> Article.obejcts.get(title="test 1").status 'PUBLISHED' >>> Article.objects.filter(status="DRAFT") <QuerySet > >>>
As, we can see here there were two obejcts
objects with the status as
. By iterating over the queryset and assigning the status of the object to published, the query set was changed and modified locally.
By using the
method, we parsed the queryset and the list of attributes to be updated into the function. This gives us the number of objects which were updated, in this case
. We then look into the article actual record in the database and it has indeed updated to the value we set in this operation.