I have a pretty generic Article model, with m2m relation to Tag model. I want to keep count of each tag usage, i think the best way would be to denormalise count field on Tag model and update it each time Article being saved. How can i accomplish this, or maybe there’s a better way?
Share
You can do this by creating an intermediate model for the M2M relationship and use it as your hook for the
post_saveandpost_deletesignals to update the denormalised column in theArticletable.For example, I do this for favourited
Questioncounts in soclone, whereUsers have a M2M relationship withQuestions:There’s been a bit of discussion on the django-developers mailing list about implementing a means of declaratively declaring denormalisations to avoid having to write code like the above: