Hi all,
I'm using a Modelform for User to edit first_name and last_name, but I want an extra field from UserProfile (which has a 1-to-1 relation with User) added to this.
class UserProfileInfoForm(forms.ModelForm):
""" Profile Model field specifications for edit.
"""
class Meta:
model = User
fields = ('first_name', 'last_name', 'user_phonemobile')
first_name = forms.CharField(
label = _('First name'),
max_length = 30,
required = True
)
last_name = forms.CharField(
label = _('Last name'),
max_length = 30,
required = True
)
user_phonemobile = forms.CharField(
label = _('Mobile phone'),
max_length = 15,
required = False,
)
I have the extra field in the ModelForm (and displayed on the page) but I can't get it to populate with the value from the other model (UserProfile). Been trying to do it subclassing the init of the modelform but no luck so far.
I could ofcourse make it a normal form and populate it like this in my view
userdata = request.user.get_profile()
data = {
'first_name': request.user.first_name,
'last_name': request.user.last_name,
'user_phonemobile': userdata.user_phonemobile,
}
profileinfoform = UserProfileInfoForm(data)
But when adding extra fields in the future it feels like this will clutter my view definition.
Regards,
Gerard.