I have a model, let's save A, the definition is as below:
class A(models.Model):
name = models.CharField('name', max_length=10)
enabled = models.BooleanField('enabled', default=False)
field1 = models.CharField('field1', max_length=10)
field2 = models.CharField('field2', max_length=10)
field3 = models.CharField('field3', max_length=10)
parent = models.ForeignKey('self', null=True, blank=True) # hierarchy
and some object instances of this model, a, b, c, d. the hierarchy is represented by the parent field.
a
|-- b
|-- c
|-- d
So, what I need to do is, when b.enabled
change from False
to True
, or vice versa, I need to update c.enabled
and d.enabled
to value: b.enabled
.
That's say, I need to broadcast the change to the children instances when the parent's enabled
field was changed.
For performance consideration, I only need to broadcast the changes when enabled
field is really changed. And don't want to always update the child instance whenever the parent is save, e.g. only updating field1 or field2.
So, do anyone knows, what is the best way to implement this logic? Thanks in advance.