简体   繁体   English

Django处理MultipleChoiceField数据花费的时间太长

[英]Django processing MultipleChoiceField data takes too long

I'm new in Django and got problems with high POST-request time of MultipleChoiceField. 我是Django的新手,并且MultipleChoiceField的POST请求时间过长而遇到问题。 It has list of persons, and i want to display list of addresses, where these persons lived with time they lived there, but my solution is too slow. 它有人员列表,我想显示地址列表,这些人在这里住的时间和他们住的时间一样,但是我的解决方案太慢了。

So if i select 3+ persons load-time is becoming very big (4-5 seconds), but db queries takes only 0.5-0.7 seconds (as django-debug-toolbar says), thats why I didnt created any indexes yet (I will create them). 因此,如果我选择3个以上的人,加载时间变得非常大(4-5秒),但是数据库查询仅花费0.5-0.7秒(如django-debug-toolbar所说),那就是为什么我还没有创建任何索引的原因(我将创建它们)。 I think the problem of long page load is about my wrong view work. 我认为长页面加载的问题与我的错误视图工作有关。 Also in 'Timer' panel of django-debug-toolbar most of the time takes "Request" section. 同样在django-debug-toolbar的“ Timer”面板中,大部分时间都使用“ Request”部分。

simplified version of my models.py: 我的models.py的简化版:

class Person(models.Model):
    name = models.CharField(max_length=300)

class House(models.Model):
    name = models.CharField(max_length=300)
    persons = DictField() # stores {person_id: time_lived} - persons, who lived here

forms.py: forms.py:

class PersonListForm(forms.Form):
    persons= forms.MultipleChoiceField(choices=
        ((person['id'], person['name'].lower()) for person in sorted(
        Person.objects.all().values('id', 'name'), key=itemgetter('name'))),
    label='Choice person list:'
)

views.py: views.py:

class ChoicePersonView(FormView):
    template_name = 'guess_houses.html'
    form_class = PersonListForm

    def get(self, request, *args, **kwargs):
        form = self.form_class(initial=self.initial)
        return render(request, self.template_name, {'form': form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            persons_id = form.cleaned_data['persons']
            houses = [] # list of houses i want to display
            t1 = time()
            comb = [] # I need all houses, where lived any of these persons
            for r in range(1, len(persons_id )+1):
                comb += [i for i in combinations(persons_id ,r)]
            for id_person_dict in House.objects.all().values('id', 'persons', 'name'):
                for c in comb:
                    if set(c).issubset(set(id_person_dict['persons'])): 
                        if id_person_dict not in houses:
                            houses.append(id_person_dict)
        return render(request, self.template_name, {'form': form, 'houses': houses, 't': time() - t1})

I'm asking for any advices to optimize my app, thank you! 我需要任何建议来优化我的应用程序,谢谢!

First you can sort using sql, and then for the casing you can do it with css instead. 首先,您可以使用sql进行排序,然后对于外壳,可以使用CSS代替。

((person['id'], person['name'].lower()) for person in sorted(
        Person.objects.all().values('id', 'name'), key=itemgetter('name'))),

BECOMES 成为

    forms.MultipleChoiceField(choices=Person.objects.order_by('name').values('id', 'name'))

With CSS:
.lowercase {
    text-transform: lowercase;
}

For the form saving part can you post the model of Person and House? 对于表单保存部分,您可以发布人员和房屋模型吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM