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

标签 python django django-forms django-views django-registration

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

我已经使用 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/

相关文章:

Python 更新全局变量

python - matplotlib/ basemap 没有河流的世界地图?

django - 没有提交文件。检查表单上的编码类型

python - Django - 如何预填充管理表单字段

python - 奇异矩阵的高效和pythonic检查

python - for line in json 对象每行返回一个字符

python - sqlalchemy 通用外键(就像在 django ORM 中一样)

python - Django 在函数内重定向

python - 用于 django 中迁移的自动数据库路由器

Django Form 将 ChoiceField 选项设置为 Form 被调用