Note that there are some explanatory texts on larger screens.

plurals
  1. POEdit profile with ModelForm and UserProfile
    text
    copied!<p>I use extended UserProfile. Now I want to make a profile edition. I need to place all my custom profile fields and email, firstname, lastname from original User model. I try to do this, but can't make it work. Email field is not shown. None of the User model are shown.</p> <p>My forms:</p> <pre><code>class MainUserProfile(ModelForm): class Meta: model = User fields = ('email',) class UserProf(MainUserProfile): class Meta: model = UserProfile </code></pre> <p>My view:</p> <pre><code>form = UserProf(instance=request.user.get_profile()) </code></pre> <p><strong>UPDATE:</strong></p> <p>I made it:) Here's the code: </p> <pre><code>class EditCustomerForm(ModelForm): def __init__(self, *args, **kwargs): super(EditCustomerForm, self).__init__(*args, **kwargs) try: 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 except User.DoesNotExist: pass required_css_class = 'required' error_css_class = 'error' email = forms.EmailField(label=_(u"Email")) first_name = forms.CharField(max_length=30, required=True, label=_(u'Forname')) last_name = forms.CharField(max_length=30, required=True, label=_(u'Surname')) address = forms.CharField(max_length=255, min_length=10, required=True, label=_(u'Address')) class Meta: model = UserProfile fields = ('email', 'first_name', 'last_name', 'address') def clean_email(self): return check_email(self.cleaned_data['email'], self.instance.user.id) def save(self, *args, **kwargs): u = self.instance.user u.email = self.cleaned_data['email'] u.save() profile = super(EditCustomerForm, self).save(*args,**kwargs) return profile </code></pre> <p>a form in a view:</p> <pre><code>if request.method == 'POST': form = EditCustomerForm(request.POST, instance=user) else: form = EditCustomerForm(instance=user) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload