- 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 5 years, 8 months ago
- User Profile minimal code by Natim 5 years ago
- Quick line profiler decorator by ibestuzhev 1 year, 3 months ago
- filter dates to user profile's timezone by Scanner 9 years, 4 months ago
- Simple profile middleware by limodou 9 years, 5 months ago