I have expanded the standard user model with the UserProfile model with the following fields:
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
gender = models.CharField(max_length=30)
info = models.CharField(max_length=30)
but when creating a new user using this form:
class SignupForm(forms.Form):
first_name = forms.CharField(max_length=30, label='First name')
last_name = forms.CharField(max_length=30, label='Last name')
info = forms.CharField(max_length=30, label='info')
def save(self, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
profile = UserProfile()
profile.user = user
profile.info = self.cleaned_data['info']
profile.save()
user.profile = profile
user.save()
"information" is always empty ('') in the database (like any other field except id). Standard attributes (first name, last name) are saved correctly.
I tried debugging and the error is not related to SQL because the query is invalid:
INSERT INTO "user_profile" ("user_id", "info") VALUES (17, '',
'') RETURNING "user_profile"."id";
But, when I debug the value of user.profile.info using pdb, it works correctly:
(Pdb) user.profile.info
u'info passed from form'
source
share