I am doing agressive caching and I’d like to make sure that nobody accidentally writes code that updates the database directly. I would imagine that the way to solve this would be to rename the normal object manager .rw_objects for the caches use, and replace .objects with a manager that logs a warning on a non-updating access and throws an exception if someone tries to do an update from it.
I’ve written an object manager and a query set but I’m not sure how to go about checking whether a query is updating the database.
Any suggestions?
I’m thinking about two approaches here.
Create a custom Manager, override
_insert(),_update()to raise exception / log the query, andget_query_set()to return custom QuerySet that overridescreate(),get_or_create(), andupdate().If you’re using django 1.2, Create another database connection in
settings.pycall it"READ_ONLY", and create a custom manager that returns QuerSet using that connection (likedef get_query_set() return super(ReadOnlyManager, self).get_query_set().using("READ_ONLY"), and mark the connection read-only. (One way to do it is create read-only user for the database connection"READ_ONLY"…. If you’re using Postgres, you can do stuff like How do you create a read-only user in PostgreSQL?)