gpt4 book ai didi

python - 如何允许使用 UserChangeForm Django 编辑我的扩展 UserProfile?

转载 作者:太空宇宙 更新时间:2023-11-04 05:05:50 24 4
gpt4 key购买 nike

首先是我在这里发表的第一篇实际文章,所以请保持温和。

我已经使用 Django 几个星期了,通过在这里和在谷歌上搜索已经克服了大部分麻烦,但是当我试图允许用户编辑他们的个人资料时,我似乎被卡住了。特别是允许用户编辑我扩展到普通 Django 用户的字段。

我能够允许用户查看和更新​​常规 Django 用户中包含的所有信息,但是当表单显示“company_name”字段时,它不会自动填充当前值,也不会如果用户键入不同的公司,则保存。

Forms.py

class EditUserProfileForm(UserChangeForm):
class Meta:
model = UserProfile
fields= ('company_name','password',)

class EditUserForm(UserChangeForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'password')

Views.py

def edit_profile(request):
content = {}
profile = request.user.get_username()
if request.method == 'POST':
form = EditUserProfileForm (request.POST, instance=request.user)
form2 = EditUserForm (request.POST, instance=request.user)
content['form'] = form
content['form2'] = form2

if form.is_valid() and form2.is_valid():
new_user = form.save()
new_user2 = form2.save()
return render(request, 'website/view_profile.html', content)

else:
content['form.errors'] = form.errors
content['form2.errors'] = form2.errors
else:
form = EditUserProfileForm(instance=request.user)
form2 = EditUserForm(instance=request.user)
content['form']= form
content['form2'] = form2
return render(request, 'website/edit_profile.html', content)

Models.py(不确定是否有必要)

class UserProfile(models.Model):
user = models.OneToOneField(User)

verified_email = models.BooleanField(blank=True, default = False)
company_name = models.CharField(max_length=100, blank=False)

def __unicode__(self):
return self.user.username

如果可能需要任何其他代码片段来帮助我,请告诉我。

谢谢!

最佳答案

所以对于遇到这个问题的其他人,我找到了解决方案。

在我的 views.py 中我不得不改变这个:

form = EditUserProfileForm(instance=request.user)

为此:

form = EditUserProfileForm(instance=request.user.userprofile)

这最终使我能够查看我的扩展 UserProfile 信息。然而,忽略更改它我不得不覆盖 UserChangeForm 的 clean_password 方法,最终只是从 UserChangeForm 中删除了我需要的内容并结束了这个:

Forms.py

class EditUserProfileForm(forms.ModelForm):
"""
This class is a carbon copy of the UserChangeForm class from
django.contrib.auth.forms, with the password functionality deleted, and
the form is modified to allow changes to be made to the
UserProfle, which extends the Django User
"""
class Meta:
model = UserProfile
fields = ('company_name',)

def __init__(self, *args, **kwargs):
super(EditUserProfileForm, self).__init__(*args, **kwargs)
f = self.fields.get('user_permissions', None)
if f is not None:
f.queryset = f.queryset.select_related('content_type')

class EditUserForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')

def __init__(self, *args, **kwargs):
super(EditUserForm, self).__init__(*args, **kwargs)
f = self.fields.get('user_permissions', None)
if f is not None:
f.queryset = f.queryset.select_related('content_type')

关于python - 如何允许使用 UserChangeForm Django 编辑我的扩展 UserProfile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44509497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com