Fist of all this is my first actual post here, so please be gentle.
I have been working with Django for a few weeks now and have overcome most troubles by searching here and on Google, however I seem to be stuck when trying to allow a user to edit their profile. Specifically allowing a user to edit the field that I extended to the normal Django User.
I am able to allow the user to view and update all of the info contained in the regular Django User, however when the form displays the "company_name" field, it does not auto-populate with the current value and it does not save if the user types a different company in.
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 (not sure if this is necessary)
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
If there are any other code snippets that might be needed to help me out please let me know.
Thanks!
So for anyone else who comes across this problem, I found the solution.
In my views.py I had to change this:
form = EditUserProfileForm(instance=request.user)
to this:
form = EditUserProfileForm(instance=request.user.userprofile)
This finally enabled me to view my extended UserProfile information. However ignorer to change it I had to overwrite the clean_password method of UserChangeForm, and ultimately just ended up cutting what I needed from the UserChangeForm and ended up with this:
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')