我开始学习python和django
框架,并弄乱了从UserChangeForm
导入的django.contrib.auth.forms
,现在尝试同时更新用户信息和用户个人资料结合两个字段。
这是我的forms.py
class EditProfileForm(UserChangeForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'password')
这是我的view.py
def edit_profile(request):
if request.method == 'POST':
form = EditProfileForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect('/accounts/profile')
else:
form = EditProfileForm(instance=request.user)
args = {'form': form}
return render(request, 'edit_profile.html', args)
这是我的model.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.PROTECT)
description = models.CharField(max_length=100, default='')
city = models.CharField(max_length=100, default='')
我想知道是否可以在我的EditProfileForm
类中添加字段?任何建议都会很棒!
fields = ('first_name', 'last_name', 'email', 'password','city','description')