Tane Piper wrote: > As you can see in the Branch model, there is a field called num_leafs > - I've been reading the signals documentation and had a look on the > web, but I'm still having difficulty getting my head around it. What > I want to do is when a leaf is saved, on the post_save signal I want > to increment the parent branch's num_leafs field so it contains total > number of content leafs for that branch (as a counter cache). I also > need to go reduced it by one each time a leaf is removed.
You can use the backward lookup feature of ForeignKey fields in the Django ORM. This is documented at http://www.djangoproject.com/documentation/db-api/#related-objects Update your Branch save() method to do the following: def save(self): self.num_leafs = self.leaf_set.count() self.slug = slugify(self.title) super(Branch, self).save() Regards Darryl
signature.asc
Description: OpenPGP digital signature