database - Django keep statistics up to date -


I have a Django model and I want to show some statistics about it. Like I have a field color and I want to show how many entries have their color set in "red", "green" etc.

Since I have many rows in the table and many colors I do not want to calculate the sum of each figure on each reading.

So I tried to come up with a second table containing the amount of calculation but now I have to keep this table synchronized with the original table. As soon as an entry is added, deleted, or modified, I have to update the statistics table.

How do I do something like this?

Choose count (ID) depending on your line number ... GROUP BY < / Code> is usually fast.

Otherwise, it may be very interesting concept for this purpose to 'listen' or use your custom signal to some events like saved / deleted models.

In your case, you want to listen to the post_save and post_dateep signal.

An example is: django.db.models import signal import model dff items saved from ( < P>

Edit : See also for a single SQL operation

Comments