如何减少对数据库的查询?

时间:2014-05-03 07:43:41

标签: python django

请告诉我如何以较短的形式将查询写入数据库。 views.py:

@login_required
def personal_data_page(request):            
    if request.method == 'POST':
        form = PersonalDataForm(request.POST)
        if form.is_valid(): 
            id_user = User.objects.get(username__exact=request.user.username)
            entry_user_profile = UserProfile.objects.get(user_ptr_id__exact=id_user)
            entry_user_profile.family = form.cleaned_data.get('family')
            entry_user_profile.name1 = form.cleaned_data.get('name1')
            entry_user_profile.name2 = form.cleaned_data.get('name2')
            entry_user_profile.nickname = form.cleaned_data.get('nickname')
            entry_user_profile.gender = form.cleaned_data.get('gender')
            entry_user_profile.status = form.cleaned_data.get('status')
            entry_user_profile.city = form.cleaned_data.get('city')
            entry_user_profile.phone = form.cleaned_data.get('phone')
            entry_user_profile.skype = form.cleaned_data.get('skype')
            entry_user_profile.email_address = form.cleaned_data.get('email_address')
            entry_user_profile.other = form.cleaned_data.get('other')
            entry_user_profile.save()   
            return HttpResponseRedirect('/userprofile/personal_data_page_changed/')
    else:
        form = PersonalDataForm()

    t = loader.get_template('personal_data_page.html')
    c = RequestContext(request, {
        'form': form,
    }, [custom_proc])
    return HttpResponse(t.render(c))    

这里我使用11行记录在数据库中。但我需要写更短的查询

0 个答案:

没有答案