Login

UserProfileForm

Author:
Natim
Posted:
November 13, 2009
Language:
Python
Version:
1.1
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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.