i’m using Django registration, and unlike everybody else, I seem to have the opposite problem. My User object is saved fine, but my UserProfile object isn’t!
I followed this website:
http://birdhouse.org/blog/2009/06/27/django-profiles/
which was really good, and so now i have:
class ProfileForm(forms.ModelForm):
YESNO = [
(True,mark_safe('<img src="/static_files/greenTick.png"/>')),
(False,mark_safe('<img src="/static_files/redCross.png"/>'))]
class Meta:
model = UserProfile
exclude = ('isTweeting','points','user')
fields = ('display_name','first_name','last_name','email','gravatar')
def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)
self.fields['email'].initial = self.instance.user.email
self.fields['first_name'].initial = self.instance.user.first_name
self.fields['last_name'].initial = self.instance.user.last_name
self.fields['display_name'].initial = self.instance.user.username
self.fields['gravatar'].initial = self.instance.usesGravatar
#add in the input to size it juuuuust right.
email = forms.EmailField(label="Primary email",help_text='',widget=forms.TextInput(attrs={'class': 'wideInput'}))
first_name = forms.Field(label="First name",help_text='',required=False,widget=forms.TextInput(attrs={'class': 'wideInput'}))
last_name = forms.Field(label="Last name",help_text='',required=False,widget=forms.TextInput(attrs={'class': 'wideInput'}))
display_name = forms.Field(label="Display name",help_text='',widget=forms.TextInput(attrs={'class': 'wideInput'}))
gravatar = ImgModelChoiceField(label='Gravatar', choices=YESNO, widget=forms.RadioSelect(renderer=ImgRadioFieldRenderer))
def save(self, *args, **kwargs):
"""
Update the primary email address on the related User object as well.
"""
u = self.instance.user
u.email = self.cleaned_data['email']
u.username = self.cleaned_data['display_name']
u.first_name = self.cleaned_data['first_name']
u.last_name = self.cleaned_data['last_name']
u.save()
self.instance.gravatar = (self.cleaned_data['gravatar'] == 'True')
profile = super(ProfileForm, self).save(*args,**kwargs)
return profile
this object is passed into the Django-profile as the form_class for use, as described in the website above. The problem i have is that when i submit my form, while the “User” data is updated correctly – any changes in the email or whatnot propagate to the db – the change to the “gravatar” value is not sent. Also no error is thrown.
Any ideas what I should do?
I’m going to hazard a guess here
It seems you are using a custom widget, and by the looks of things you need to change the string
'True'(passed back from the form) to a booleanTruebefore saving it to the DB. When you callsave()on the next line though, theModelFormwill overwrite the value you have givenself.instance.gravatarwith the data directly from the form’scleaned_data:https://github.com/django/django/blob/master/django/forms/models.py#L351
Also, in
__init__, you don’t need to includeas this field is already bound to the model form and will be automatically populated (if the
UserProfileis being edited for example) when you instantiate the form along with an instance in your view.Finally, in your
Meta, you don’t need to include bothexcludesandfields, one or the other should be fine.