id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux 14121 Provide object's primary key as a hint for database router method db_for_read() when filtering objects by primary key sandy@… nobody "'''Problem:''' Setting up a custom database router with db_for_read() method to allow for sharding with MySQL. Need to be able to control what database an object is read from on a per-object basis. [[BR]] [[BR]] '''Example Case:''' I want to retrieve User objects from the database cluster as follows: * If the username starts with a number, read from slave_db_1 * If the username starts with a letter, read from slave_db_2 [[BR]] Currently, the only way to achieve this behavior is to specify ""using"" on each condition, and per each call to the database. [[BR]] [[BR]] '''Proposed Solution:''' [[BR]] [[BR]] When filtering objects by primary key, pass the primary key to db_for_read() as a hint. [[BR]] [[BR]] '''Example Code:''' {{{ #View Code: """"""Assume username is pk for User model"""""" users = User.objects.get(username=""testuser"") #DB Router Code class UserReadRouter(object): def db_for_write(self, model, **hints): return 'master' def db_for_read(self, model, **hints): """"""Assume pk of User object is a string"""""" if model.__name__ == ""User"" and 'object_pk' in hints: if hints['object_pk'][0].isalpha(): return 'slave_db_2' else: return 'slave_db_1' }}} [[BR]] [[BR]] The end result is that ""testuser"" is retrieved from slave_db_1." New feature closed Database layer (models, ORM) 1.2 Normal wontfix router, hints noah@… Design decision needed 0 0 0 0 0 0