I'm using a multi-db in my Django app with a master database and a read replica, but to avoid replication lag problems the router always uses default database except few places where I set DB manually.
I'm facing an issue as I don't know how to specify the database used for prefetch_related.
For example I want the next query to use only read_replica
DB, but it does 2 queries, the first goes to read_replica
as expected and the second goes to default
DB.
users = UserProfile.objects.using('read_replica').prefetch_related('usermedia_set').filter(id__in=user_ids)
This are the output of this query:
SELECT @@SQL_AUTO_IS_NULL; args=None
SELECT VERSION(); args=None
SELECT ... FROM
ftmanager_userprofile
WHERE (ftmanager_userprofile
.id
IN (33); args=(33,)SELECT @@SQL_AUTO_IS_NULL; args=None
SELECT VERSION(); args=None
SELECT ... FROM
ftmanager_usermedia
WHEREftmanager_usermedia
.user_id
IN (33); args=(33,)
I see a related ticket on Django tickets but I do not understand how to apply using()
to inner queryset.
I have found the solution thinking about the Django ticket, you need to use the Prefetch
class to define the inner queryset used by prefetch_related, it really messes the code but the gain deserves it:
from django.db.models.query import Prefetch
users = UserProfile.objects.using('read_replica').prefetch_related(Prefetch('usermedia_set', queryset=UserMedia.objects.using('read_replica'))).filter(id__in=user_ids)
I've encountered the same problem. Configuring the router (namely, db_for_read and using instance from hints) helped:
def db_for_read(self, model, **hints):
instance = hints.get('instance')
if instance is not None:
if instance._meta.app_label in self.route_app_labels:
return self.route_db_name
elif model._meta.app_label in self.route_app_labels:
return self.route_db_name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.