Login

UserProfileForm

Author:
Natim
Posted:
November 13, 2009
Language:
Python
Version:
1.1
Tags:
user profile form
Score:
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

  1. notify admin what fields have changed in form submission by pjv 4 years, 3 months ago
  2. User Profile minimal code by Natim 3 years, 7 months ago
  3. filter dates to user profile's timezone by Scanner 7 years, 11 months ago
  4. Simple profile middleware by limodou 8 years ago
  5. mask_email filter by jkocherhans 8 years ago

Comments

Please login first before commenting.