django update query code example

Example 1: update in django orm

# Update all the headlines with pub_date in 2007.
Entry.objects.filter(pub_date__year=2007).update(headline='Everything is the same')

Example 2: django update model

Model.objects.filter(id = 223).update(field1 = 2)

Example 3: django delete object

SomeModel.objects.filter(id=id).delete()

Example 4: update queryset in django

>>> Entry.objects.filter(pub_date__year=2010).update(comments_on=False)

Example 5: how to work with django ornm __in

# Get blogs entries with id 1, 4 and 7
>>> Blog.objects.filter(pk__in=[1,4,7])

# Get all blog entries with id > 14
>>> Blog.objects.filter(pk__gt=14)

Tags:

Misc Example