Django: Database used for prefetch_related is not the same that the parent query

时光怂恿深爱的人放手 提交于 2019-12-11 05:42:40

问题


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 WHERE ftmanager_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.


回答1:


I have found the solution thinking about the Django ticket, you need to use thePrefetch 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)


来源:https://stackoverflow.com/questions/40139674/django-database-used-for-prefetch-related-is-not-the-same-that-the-parent-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!