I have a register form with fields(firstname, lastname, username, email, dateofbirth, phoneno.)
I have used signals to create a record in userprofile. whenever a new userdata is added to the user table
what i want is whenever user post the request. the data should get saved in user table and then the signal creates a record in profile table and then the remaining data should also get populated in there respected fields. As I'm receiving all the data from user at the registration time.
views.py
def register(request):
if request.method == "POST":
print(request.POST)
first_name = request.POST.get('first-name') <-----user table
last_name = request.POST.get('last-name') <-----user table
username = request.POST.get('username') <-----user table
email = request.POST.get('email') <-----user table
gender = request.POST.get('gender') <-----profile table
date_of_birth = request.POST.get('date-of-birth') <-----profile table
address = request.POST.get('address') <-----profile table
phone_number = request.POST.get('mob-number') <-----profile table
ans_1 = request.POST.get('ans-1') <-----profile table
ans_2 = request.POST.get('ans-2') <-----profile table
User.objects.create_user(first_name=first_name, last_name=last_name, email=email, username=username, last_login=timezone.now())
return render(request, 'authentication/register.html')
signals.py
@receiver(post_save, sender=User)
def create_user_info(sender, instance, created, **kwargs):
if created:
UserInformation.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_info(sender, instance, **kwargs):
instance.userinfo.save()
apps.py
class YourappConfig(AppConfig):
...
def ready(self):
import yourapp.signals