I've got simple project, that do almost nothing now - there is MultipleChoiceField with some Person-values and when user selects some values and clicks "submit" button - I'm sarching for Houses, where selected guys lived.
There is about 1.5k persons and 20k houses in my database. If I select 3+ people from list (in this case, there is about 5-6k values in 'houses' list), processing time is taking very long time (~5-7 seconds for my 't' variable).
As django-debug-toolbar says, DB-queries takes only 0.7 seconds and ~95% of time is 'request' section of 'Timer' panel. So the question is - what am I doing wrong? I think there is wrong way of using render_to_response. Can you recommend me something to optimize my code? I'm new at Django, thank you.
At my 'guess_houses.html' I only display list of houses with their field.
Here is my views.py:
class HouseView(FormView):
template_name = 'guess_houses.html'
form_class = PersonListForm # that's my MultipleChoiceField with persons in it
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
persons_id = form.cleaned_data['persons']
houses= []
t1 = time()
for id_person_dict in House.objects.all().values('id', 'persons', 'address'):
# if persons_id and persons who lived in house got intersection element(s)
if set(persons_id) & set(id_person_dict['persons']):
if id_person_dict not in houses:
houses.append(id_person_dict)
return render_to_response(self.template_name, {'form': form, 'houses': houses, 't': time()-t1})