GistDom Blog
GistDom Blog

Reputation: 51

Django Allauth saving profile information with signup form

I am new in Django, I am using allauth app to create a user registration. There are some extra field I wish to have in my signup form. Not only (username, first_name, last_name) I wish to also include info to the registration form.
When I submit the registration form only the first_name and the last_name are saved in database, info is not saved. I guess it should be saved in Profile Model, but its not there.

class Profile(models.Model):
    user = models.OneToOneField(User)
    info = models.CharField(max_length=128)

class CustomSignUpForm(Signup Form):
    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=30)
    info = forms.CharField(max_length=50)

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()
        user.profile.info = self.cleaned_data['info']
        user.profile.save()


ACCOUNT_FORM = 'signup' 'myapp.forms.CustomSignupForm'

Upvotes: 0

Views: 292

Answers (1)

neeraj9194
neeraj9194

Reputation: 373

Are you sure the Profile object exists for that user and you are not getting RelatedObjectDoesNotExist error in logs? If yes then you have to create a Profile object, in the above signup form.

Profile.objects.update_or_create(user=user, defaults={"info":self.cleaned_data['info']})

Upvotes: 0

Related Questions