Answer the question
In order to leave comments, you need to log in
How to customize user creation in Django?
Django has standard user creation tools, I used
this trick before
class SignUp(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
class SignUpFormTut(UserCreationForm):
role = forms.ChoiceField(
choices = (
('P','Родитель'),
('S','Ученик'),
)
)
class SignUpTut(generic.CreateView):
form_class = SignUpFormTut
success_url = reverse_lazy('login')
template_name = 'signup.html'
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
Answer the question
In order to leave comments, you need to log in
You can add logic to the method form_valid
to create a profile. Look in the docks what this method does (it only creates a User + returns a response)
You can also send a signal to the user's save post. But signals are harder to test and debug. It can be something like this:
def create_profile(sender, instance, created, **kwargs):
""" Creates profile for user """
CustomerProfile.objects.get_or_create(user=instance)
post_save.connect(create_profile, sender=USER_MODEL)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question