如何在一个视图中更新两个表单,其中一个表单是userChangeform(djangoform),另一个表单是modelform(模型是userextensionmodel)?

如何在一个视图中更新两个表单,其中一个表单是userChangeform(djangoform),另一个表单是modelform(模型是userextensionmodel)?,django,django-models,django-forms,django-views,django-templates,Django,Django Models,Django Forms,Django Views,Django Templates,如果我不能正确地提出我的问题,我道歉。 我希望能够在同一页上显示和编辑联系人号码以及用户名、姓氏和名字等其他详细信息。我尝试过不同的方法,比如使用query从数据库调用,并在同一个函数上保存表单和查询,但它没有更新联系人号码。这可以做到吗?或者我必须采取一些不同的方法吗?如果可以做到,请让我知道?如果我遗漏了一些细节,请让我知道,以防有问题,因为这是我第一次在stack询问 views.py @login\u required() def编辑_配置文件(请求): 如果request.method

如果我不能正确地提出我的问题,我道歉。 我希望能够在同一页上显示和编辑联系人号码以及用户名、姓氏和名字等其他详细信息。我尝试过不同的方法,比如使用query从数据库调用,并在同一个函数上保存表单和查询,但它没有更新联系人号码。这可以做到吗?或者我必须采取一些不同的方法吗?如果可以做到,请让我知道?如果我遗漏了一些细节,请让我知道,以防有问题,因为这是我第一次在stack询问

views.py

@login\u required()
def编辑_配置文件(请求):
如果request.method==“POST”:
form=EditProfileForm(request.POST,instance=request.user)
如果form.is_有效():
user=form.save()
profile=user.profileModel()
profile.contactNumber=表单.u数据['contactNumber']
profile.save()
返回HttpResponseRedirect(反向('profile'))
其他:
form=EditProfileForm(实例=request.user)
返回渲染(请求'music/edit_profile.html',{'form':form})
models.py

class profileModel(models.Model):
user=models.OneToOneField(用户,on_delete=models.CASCADE)
contactNumber=models.IntegerField(验证器=[validate\u偶数])
#canPOST=models.BooleanField(默认值=False)
定义(自我):
返回“用户{}的配置文件”。格式(self.user.username)
用于编辑配置文件功能的Html文件


{%csrf_令牌%}
用户名
必修的。150个字符或更少。仅限字母、数字和@/+/-/-。
名字
姓
编辑配置文件
Forms.py

class EditProfileForm(UserChangeForm):
password=forms.CharField(label='',widget=forms.TextInput(attrs={'type':'hidden'}))
contactNumber=forms.CharField()
类元:
模型=用户
字段=['username',
“名字”,
“姓”,
“密码”,
]
#用户额外信息。。。。。。。。。。。。。。。。
类配置文件InformForm(forms.ModelForm):
类元:
模型=轮廓模型
字段=['contactNumber']

试试这张,你只需要一张表格,而不是两张。配置文件模型中的联系人号码应该是CharField,而不是IntegerField

# models.py

class ProfileModel(models.Model):
    user = models.OneToOneField(User, on_delete= models.CASCADE)
    contact_ number = models.IntegerField(validators=[validate_even])

    def __str__(self):
        return 'Profile for user {}'.format(self.user.username)


# forms.py
class EditProfileForm(UserChangeForm):
    password = forms.CharField(label='', widget = forms.TextInput(attrs = {'type' : 'hidden'}))
    contact_number = forms.CharField()

    class Meta:
        model = User
        fields = ['username',
                  'first_name',
                  'last_name',
                  'password',
                  ]


# views.py
@login_required()
def edit_profile(request):
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)
        if form.is_valid():
            user = form.save()
            # profile should be one2one with profile model and then you can easily do this
            # profile = user.profile
            # with ForeignKey
            profile = user.profilemodel_set.first()  # or if has many, get the one you need to update.
            profile.contact_number = form.cleaned_data['contact_number']
            profile.save()
            return HttpResponseRedirect(reverse('profile'))
    else:
        form = EditProfileForm(instance = request.user)
    return render(request, 'music/edit_profile.html', {'form' : form})

你应该在问题中发布代码,而不是重定向到图像。你好,jabez,我现在已经发布了代码。我按照你的建议执行了相同的操作,当我尝试保存联系人号码时,它给了我以下错误:“AttributeError at/edit_profile/‘User’对象没有属性‘profile’”. 此外,在用户注册过程中,联系人号码甚至在此之前就已存储,因为这是一个必填字段,所以我希望它仅为整数字段。您收到此错误是因为您尚未从FK切换到O2O,我已更新了代码段,请立即检查。关于电话号码,您可以验证为int或其他形式,但您应该将其保存在db中作为文本,除非您以后不打算对其进行数学运算。实际上,我已将代码从FK更改为One2One,但最终还是得到了相同的错误。我认为,因为我使用用户类作为抽象。你认为这可能是我得到AttributeError的原因吗?我还尝试了您新更新的代码,但仍然收到相同的错误。请尝试使用user.profilemodel,而不是user.profile。这是一个非常简单的问题,很容易解决。你看过所有的django文档了吗?这部分与您遇到的问题有关。我理解你要求我做什么,我也做了同样的事情,并且尝试了,但是错误仍然没有发生。我已经更新了问题中的代码片段,所以如果我还在某个地方出错,请告诉我。如果这花了你一些时间,我很抱歉,是的,我确实读了这些文件,也读了一份有一份文件的文件。