Sunday, 11 August 2013

At what point does django write an object to the database?

At what point does django write an object to the database?

I am overriding the save method on one of my Models like so:
class Foo(models.Model):
...
def save(self, *args, **kwargs):
request = kwargs.pop('request', None)
super(Foo, self).save(*args, **kwargs)
some_stuff()
some_stuff() performs some queries which expect that the new/modified Foo
object has been saved to the database (which I why I've put it after the
super() call). However, I'm finding when some_stuff() runs the newly
created/modified object is not present in the DB.
Is my understanding of when things are written to the DB incorrect? How
else might I do this (I've considered signals, but all of this is within
the same app, so that seems overkill)?
UPDATE: I've tried putting a signal receiver to see if that makes any
difference; in fact it gets called before the super() call finishes, so
the DB state is no different whether I override save() use a signal.

No comments:

Post a Comment