3

In Postgresql you can issue an UPDATE with a RETURNING statement in order to fetch some columns after you perform an update. This is useful because you can do this in one trip to the database instead of two.

UPDATE foo
SET bar = 10
WHERE id = 1
RETURNING baz

How can I simulate this in Django without using a raw query ?

I have seen some older questions on stackoverflow from a few years ago indicating this was not possible. Has anything changed in recent years?

I do not want to do the following, which requires two trips to the database:

foo = Foo.objects.get(id=1)
foo.bar = 10
foo.save()
print(foo.baz)

I did see this ticket from four years ago which was rejected. Has anything changed?

Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231

0 Answers0