0

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})
vadimb
  • 462
  • 3
  • 14
  • `houses` should probably be a set (the not in would perform much faster). Also, you can create the `set(persons_id)` beforehand - no need to do that in the loop multiple times. Does these tweaks make things faster? – alecxe Apr 02 '16 at 13:57
  • @alecke thank you for advice! Unfortunately, houses can't be set, because 'id_person_dict' is a dict, which is not hashable type in python. Anyway, I made this work with values_list function, which makes list, but processing time didnt change. Same thing with set(persons_id). – vadimb Apr 02 '16 at 14:01
  • I'm not sure why you think this question has anything to do with render_to_response. – Daniel Roseman Apr 02 '16 at 14:01
  • @DanielRoseman yes, now I'm not sure too, but my skills is Django are bad and unfortunately I don't know better ways to do it, that's why i created question. – vadimb Apr 02 '16 at 14:02
  • You need to try to move the logic to the database, see http://stackoverflow.com/questions/36299975/query-q-is-not-working-as-set-contain-for-related-field-in-django – serg Apr 02 '16 at 17:25

0 Answers0