- November 13, 2009
- user profile form
- 0 (after 2 ratings)
We often need to use a Profile form and we want to be able to modify the first_name, last_name and sometimes the email address.
Here is how I do it.
In this case I want to check the email so I did a specific form for it. But it is quite easy to add it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
class UserProfileForm(forms.ModelForm): first_name = forms.CharField(label=_(u'Prenom'), max_length=30) last_name = forms.CharField(label=_(u'Nom'), max_length=30) def __init__(self, *args, **kw): super(forms.ModelForm, self).__init__(*args, **kw) self.fields['first_name'].initial = self.instance.user.first_name self.fields['last_name'].initial = self.instance.user.last_name self.fields.keyOrder = [ 'first_name', 'last_name', '...some_other...', ] def save(self, *args, **kw): super(forms.ModelForm, self).save(*args, **kw) self.instance.user.first_name = self.cleaned_data.get('first_name') self.instance.user.last_name = self.cleaned_data.get('last_name') self.instance.user.save() class Media: model = UserProfile
More like this
- notify admin what fields have changed in form submission by pjv 4 years, 3 months ago
- User Profile minimal code by Natim 3 years, 7 months ago
- filter dates to user profile's timezone by Scanner 7 years, 11 months ago
- Simple profile middleware by limodou 8 years ago
- mask_email filter by jkocherhans 8 years ago