We're creating a gamification component for the forum, that is being coded in Django. We wish customers to get badges immediately after achieving certain goals. However, we're worried about the quantity of database queries that might be made. For instance, have a badge that's given if your publish will get some sights. When the condition for that badge is checked each time the publish is seen, that might be lots of queries. Is our only other choice to check at certain times or any other event, such as the user viewing their profile? That might be less optimal in the user perspective, due to the delay.

There's another approach that could suit you, using webserver logging and publish-processing that log to create stats. I have tried personally it in Apache with a few projects that needed pageview hit counts. An identical configuration for other webservers works exactly the same.

I personally use django.contrib.contentypes to create lower this content Type ID and Object ID from the object utilized about this example, however, you can, obviously, log anything you like.

So, inside your Apache virtualhost conf file, adding a LogFormat directive such as this:

LogFormat "%{X-H-CID}o|%{X-H-OID}o" hitcounter

After which affixing it to some CustomLog:

CustomLog path/to/your/logfile.log hitcounter

This can enable Apache to create lower towards the logfile the next HTTP headers: X-H-CID and X-H-OID, which represent the ContentType ID and Object ID from the object being hit. From the view, you might add the headers towards the HttpResponse:

ctxt = RequestContext(request) 
rendered = render_to_string(template, ctxt)
http_res = HttpResponse(rendered) 
http_res['X-H-CID'] = content_type_id
http_res['X-H-OID'] = object_id

Replace content_type_id and object_id together with your real object props.

This situation should write a line like:

16|4353

where 16 may be the Content Type ID and 4353 the item ID. Finally, you are able to plan a django custom command to procedure that logfile and carry out the needed actions.