2

Im extending the django User model with User profile like this :

class UserProfile(BaseModel):
    user = models.ForeignKey(User, unique=True, related_name="profile")
    city = models.CharField(_("City"), max_length=200)
    tel = models.CharField(_("Phone Number"), max_length=50, 
        help_text=_("(+ Country Code) (Area Code) (Your phone number)"))
    description = models.TextField(null=True, blank=True, 
        help_text = _("Small description about yourself."))
    photo = models.ImageField(max_length=255, upload_to="profiles/")

    def __unicode__(self):
        return "%s %s" % (self.user.first_name, self.user.last_name)

    def profile_image(self):
        return settings.DEFAULT_PROFILE_IMAGE

and this is the form Im using on the front end in order to edit the profile from there:

class UserProfileForm(forms.ModelForm):

# uniForm Helper
helper = FormHelper()
helper.form_id = "edit_profile_form"
helper.form_class = 'mlForm'
layout = Layout(
    Fieldset('',
        'country', 'city',
        'tel', 'photo', 'description',
    )
)
helper.add_layout(layout)

# Submit button(s)
submit = Submit('submit','Submit')
helper.add_input(submit)
cancel = Submit('cancel','Cancel')
helper.add_input(cancel)

class Meta:
    model = UserProfile
    fields = ['city',
        'tel', 'photo', 'description',]

def __init__(self, request, *args, **kw):
    super(UserProfileForm, self).__init__(*args, **kw)
    self.request = request

def save(self, *args, **kw):
    edit_profile_form = super(UserProfileForm, self).save(*args, **kw)
    return edit_profile_form,  "User successfully modified."

What I want to do is add the User email field in the form so that the user can also edit this field from the frontend.

How would I do that?

maumercado
  • 1,453
  • 4
  • 23
  • 47

2 Answers2

3

Hope, this will help

class UserProfileForm(forms.ModelForm):  
    email = forms.EmailField(label=u'Email address',required=True)

    class Meta:
        model = UserProfile
        exclude = ['user',]

    def __init__(self, *args, **kw):
        super(UserProfileForm, self).__init__(*args, **kw)
        self.fields['email'].initial = self.instance.user.email
        self.fields.keyOrder = ['your','fields','you','want','to','display','including','email']

    def save(self, *args, **kw):
        super(UserProfileForm, self).save(*args, **kw)
        self.instance.user.email = self.cleaned_data.get('email')
        self.instance.user.save()

this recipe works for me (django 1.3). in views.py must be something like this:

def profile(request,id):
    if request.method == 'POST':
        user = get_object_or_404(User,pk=id)
        profile = user.profile
        form = UserProfileForm(data = request.POST,instance = profile)
        if form.is_valid():
            form.save()
FoRever_Zambia
  • 1,179
  • 9
  • 13
  • Hi, thanks for your help, Im getting an error when submitting the form because well the UserProfile instance does not have the email field... so how would I get the form to save the email into the User instance? – maumercado Jul 11 '11 at 17:01
  • found original answer: http://stackoverflow.com/questions/1727564/how-to-create-a-userprofile-form-in-django-with-first-name-last-name-modificatio – FoRever_Zambia Jul 11 '11 at 17:43
  • Those answer are not working for me, weird enough Im not getting any data when I use self.fields['city'].initial = self.instance.city or self.fields['email'].initial = self.instance.user.email in fact Im getting a 'UserProfile' object has no attribute 'email' – maumercado Jul 11 '11 at 21:53
0

Send the two objects-forms (UserEditForm and UserProfileForm) into the template form and submit them together.

panchicore
  • 11,451
  • 12
  • 74
  • 100