I have the following code in my view. I want to save the query result (if it's true) into another model. How to achieve this?
def scan(request):
print(request.session)
if request.method == 'POST':
srch = request.POST['srh']
if srch:
match = ReportModel.objects.filter(Q(serialNumber__iexact=srch))
if match:
return render(request, 'admin/scan.html', {'sr': match})
else:
messages.error(request, 'No result yet for the requested device!')
else:
return redirect('/scan/')
return render(request, 'admin/scan.html')
There is one way. You can create your own cache.
The logic is these steps:
AandBmodel.search_resultfield asNoneinBmodel.Amodel just checkB.search_resultvalue, if it isNonechangesearch_resultvalue to query result(Be sure that your query is executed) and return it. If it is notNonereturn value(B.search_result).AchangeB.search_resultto None.It is the simple caching your data. And when you use
all(),filter(**kwargs)methods in queryset it does not execute query, so wrap it into list or dumpt it intojson, so than change value ofB.search_result.