I want take some action right after creating a new User using UserCreationForm. In this situation i need to create a new instance of Model which is in one-to-one relationship with this new User. Please tell me how can i connect my code which will create this Model instance to the UserCreations form or to the register view. I know that there is a save method in UserCreationForm which finally creates new User, but i do not know how to connect it to my code
this is how my view looks like
def signup(request):
if request.method == "POST":
form = MyUserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
form = MyUserCreationForm()
return render(request, 'registration/signup.html', {"form": form})
You can return the newly created user instance like this new_user=form.save()
and then you can create another model based on this user
if form.is_valid():
new_user = form.save()
YourModel.objects.create(user=new_user,....
return redirect('login')
i think signals could help you.
you can write a signal, to do something after a instance of model creates.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.