【发布时间】:2021-10-07 07:35:00
【问题描述】:
我想在 Django allauth 用户模型中添加更多字段。我创建了一个与 auth-user 一对一关系的用户配置文件模型,并尝试在 form.py 中创建用户配置文件对象。但是这种方法行不通。
根据这个文档(https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#extending-the-existing-user-model),我尝试扩展用户模型。但是注册后我没有在“UserProfile”中获得任何数据。
这是我的代码:
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='userprofile', on_delete=models.CASCADE)
profile_picture = models.ImageField()
forms.py
class CustomSignupForm(SignupForm):
profile_picture = forms.ImageField()
def signup(self, request, user):
up = user.userprofile
user.userprofile.profile_picture = self.cleaned_data['profile_picture']
up.profile_picture = self.cleaned_data['profile_picture']
user.save()
up.save()
return user
【问题讨论】:
标签: python django django-forms django-allauth