2013-10-30 78 views
0

當我嘗試插入某些值時,是否可以強制django創建子查詢? 這將產生兩個單獨的查詢:插入Django子查詢

CommunicationLog.objects.create(
    device='asdfasdf', 
    date_request=CommunicationLog.objects.get(pk=343).date_request, 
    content_obj_id=338, type_request=1, type_change=2 
) 

回答

3

你絕對不能使用create做到這一點。沒有可用的API,因爲這是非常不尋常的用例。你必須回退到原始的SQL。

0

即使API不會讓你做你想做的,你仍然可以提高性能通過查詢日期時,使用.only方法一點點(我以爲是你的意圖):

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 
)