具有此Cpf cnpj(字段(唯一))的用户个人资料信息(表)已存在

时间:2019-06-15 13:44:37

标签: django python-3.x django-models

我的查看代码是这样的:

 @login_required
    def DadosUserView(request):
        template_name = 'users_c2p/dados.html'
        usercpf = request.user.username
        profile = UserProfileInfo.objects.filter(cpf_cnpj=usercpf)
        contrato_dividas = Contrato.objects.filter(cpf_cnpj=usercpf)
        empresa = Empresa.objects.all()
        if profile:
            person = get_object_or_404(UserProfileInfo, cpf_cnpj=usercpf)
            profile_form = UserProfileInfoForm(request.POST or None, instance=person)
            flag = 1
        else:
            profile_form = UserProfileInfoForm(initial={"cpf_cnpj": usercpf,
                                                        })
            flag = 0
        if request.method == 'POST':
            profile_form = UserProfileInfoForm(data=request.POST)
            print(request.method)
            print(profile_form.errors)
            if profile_form.is_valid():
                # Create new profile
                if flag == 0:
                        profile_form_cpf = profile_form.cleaned_data['cpf_cnpj']
                        print(profile_form_cpf)
                        if usercpf == profile_form_cpf:
                            profile_form.save()
                            log = LogUsuario.objects.create(cpf_cnpj=profile_form_cpf, movimentacao="FIRST UPDATE")
                            return redirect(reverse_lazy('users_c2p:dadosuser'))
                        else:
                            return profile_form.errors
                #Update profile
                elif flag == 1:
                    profile_form_cpf = profile_form.cleaned_data['cpf_cnpj']
                    if usercpf == profile_form_cpf:
                        profile_form.save()
                        log = LogUsuario.objects.create(cpf_cnpj=profile_form_cpf, movimentacao="UPDATE")
                        return redirect(reverse_lazy('users_c2p:sucesso_cadastro'))
                    else:
                        return profile_form.errors
            else:
                return redirect(reverse_lazy('users_c2p:dadosuser'))
        context = {
                    "UserData" : profile,
                    "profile_form": profile_form,
                    "Contrato": contrato_dividas,
                    "Empresa": empresa,
                    #'media_url': settings.MEDIA_URL,
                  }
        return render(request, template_name, context)

我要使用#Update配置文件部分。 #Create配置文件运行良好,但是当我尝试对其进行更新时,它表示该用户已经存在并且不执行任何操作。

我该怎么办,以便它认识到这个主意,然后更新我想要的信息?

1 个答案:

答案 0 :(得分:0)

这段代码可以解决:

def DadosUserView(request):
    template_name = 'users_c2p/dados.html'
    usercpf = request.user.username
    profile = UserProfileInfo.objects.filter(cpf_cnpj=usercpf)
    contrato_dividas = Contrato.objects.filter(cpf_cnpj=usercpf)
    empresa = Empresa.objects.all()
    if profile:
        person = get_object_or_404(UserProfileInfo, cpf_cnpj=usercpf)
        profile_form = UserProfileInfoForm(request.POST or None, instance=person)
        flag = 1
    else:
        profile_form = UserProfileInfoForm(initial={"cpf_cnpj": usercpf,
                                                    }, )
        flag = 0
    if request.method == 'POST':
        # Create new profile
        if flag == 0:
            profile_form = UserProfileInfoForm(data=request.POST)
            print(request.method)
            if profile_form.is_valid():
                profile_form_cpf = profile_form.cleaned_data['cpf_cnpj']
                if usercpf == profile_form_cpf:
                    profile_form.save()
                    log = LogUsuario.objects.create(cpf_cnpj=profile_form_cpf, movimentacao="FIRST UPDATE")
                    return redirect(reverse_lazy('users_c2p:dadosuser'))
                else:
                    return profile_form.errors
        #Update profile
        elif flag == 1:
            print("chegou!")
            print(profile_form.errors)
            if profile_form.is_valid():
                profile_form_cpf = profile_form.cleaned_data['cpf_cnpj']
                if usercpf == profile_form_cpf:
                    profile_form.save()
                    log = LogUsuario.objects.create(cpf_cnpj=profile_form_cpf, movimentacao="UPDATE")
                    return redirect(reverse_lazy('users_c2p:sucesso_cadastro'))
                else:
                    return profile_form.errors
        else:
            return redirect(reverse_lazy('users_c2p:dadosuser'))
    context = {
                "UserData" : profile,
                "profile_form": profile_form,
                "Contrato": contrato_dividas,
                "Empresa": empresa,
                #'media_url': settings.MEDIA_URL,
              }
    return render(request, template_name, context)

当我尝试更新数据库并使用此代码时:

profile_form = UserProfileInfoForm(data=request.POST)

那是行不通的,我意识到这是当您想创建一个新条目时,为了仅进行更新,我只从表单中获取了数据。