In Django how do I notify a parent when a child is saved in a foreign key relationship?

前端 未结 2 898
情话喂你
情话喂你 2021-02-07 09:26

I have the following two models:

class Activity(models.Model):
    name = models.CharField(max_length=50, help_text=\'Some help.\')
    entity = models.ForeignKe         


        
2条回答
  •  借酒劲吻你
    2021-02-07 10:11

    What's wrong with the following?

    class Cancellation( models.Model ):
        blah
        blah
        def save( self, **kw ):
            for a in self.activity_set.all():
                a.somethingChanged( self )
            super( Cancellation, self ).save( **kw )
    

    It would allow you to to control the notification among models very precisely. In a way, this is the canonical "Why is OO so good?" question. I think OO is good precisely because your collection of Cancellation and Activity objects can cooperate fully.

提交回复
热议问题