Search code examples
djangodjango-modelsdjango-formsdjango-generic-views

form handling in Signup view as 'CreateView' with multiple values


I have 'Profile' Model like:

class Profile(models.Model):
    user = models.OneToOneField(User, related_name='user')
    nickname = models.CharField(max_length=100, name='nickname')
    avatar = models.FileField()

    def __str__(self):
        return self.nickname

    def date_joined(self):
        return self.user.date_joined

and I have a signup form like:

class SignUpForm(UserCreationForm):
    nickname = forms.CharField(label='nickname', max_length=100, help_text='Enter your nickname', required=True)
    avatar = forms.ImageField(label='Avatar', help_text='Upload your picture')

and I would like to have sign up view that get Profile of user by given form so I created a Create view like:

class SignUpView(CreateView):
    form_class = SignUpForm
    template_name = '[path_to_template]/signup.html'
    success_url = reverse_lazy('base')

but it doesn't create a Profile for same User in Profile table in database an it just make user. it's like no 'Nickname' or 'Avatar' never exist in the form. how can i get extra information about 'User' and make a 'Profile' for 'User'?


Solution

  • I found a way to solve it . I just overwrote formValid method from CreateView. as Bono mentioned

    The moment you want to make a Profile, do you already have a User?

    I just saved User and then create a new Profile for that user.