This is how my model looks like:
class Score(models.Model):
. . .
class Round(models.Model):
. . .
class Game(models.Model):
round = models.ForeignKey(Round)
. . .
def save(self):
""" Recompute the scores in the Score table """
def delete(self):
""" Recompute the scores in the Score table """
When I delete a game, with game.delete() (or through the admin,
generic_view...), delete method is called and the Score table is updated.
But when I delete a round, with round.delete(), all games conected with
that round are also deleted, but the Score table isn't updated, that is,
the delete method from the game objects is not called?!?!
> But when I delete a round, with round.delete(), all games conected with
> that round are also deleted, but the Score table isn't updated, that is,
> the delete method from the game objects is not called?!?!
Looking at the code I see that when an objects is .delete()d all the
related objects are gathered up and then it calls delete_objects:
[django/db/models/base.py]
# Actually delete the objects
delete_objects(seen_objs)
which does the actual deletion from the DB. Hence the .delete() method
wont be called for related objects.
So I'd say you weren't doing something terribly wrong, but it might
not be a bug either. If this behaviour is desirable (and I cant see why
it isn't) then it needs a bit of rewriting...
Barry
Reading the documentation and this mailing list, I came to a silly idea
that django's ORM is not about tables, columns and rows :)... It's about
objects... So, when I delete an object from my "persistent memory" (db
or whatever), and that object has a "destructor", I'm sure that it's
desirable to call that "destructor"... Or I'm missing something... :)
I'm not sure why the strategy is to gather together all the related
objects and then do the SQL rather than call the delete() method on
each of them. Perhaps its more efficient. Perhaps it avoids possible
loops where two objects refer to each other, or perhaps its all done in
one transaction to keep the DB consistent.
I can't see a simple way of doing what would seem to be the 'right
thing'. What you really need is some sort of 'pre_delete' method. Oh
dear, those things seem to have disappeared with the 'magic-removal'
code...
Probably worth filing this as a bug... If its not there already, I cant
find it!