问题
Is it possible to force django to make a subquery when I try to insert some values? This produces two separate queries:
CommunicationLog.objects.create(
device='asdfasdf',
date_request=CommunicationLog.objects.get(pk=343).date_request,
content_obj_id=338, type_request=1, type_change=2
)
回答1:
You definitely cannot do it by using create
. There's no available API that will let you do it, since this is very unusual use case. You have to fall back to raw sql.
回答2:
Even if the API won't let you do what you want, you can still improve the performance a little bit (which I assume is your intention) by using the .only method when querying the date:
CommunicationLog.objects.create(
device='asdfasdf',
date_request=CommunicationLog.objects.only('date_request').get(343).date_request,
content_obj_id=338, type_request=1, type_change=2
)
来源:https://stackoverflow.com/questions/19686559/django-subquery-in-insert