Search code examples
djangosubquerydjango-orm

Django subquery in insert


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
)

Solution

  • 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.