Skip to content
Advertisement

How to save some data in user model and some data save in extended user model from a html form

model.py

from django.db import models

from django.contrib.auth.models import User

from django.db.models.signals import post_save

from django.dispatch import receiver

from django.db.models.fields import CharField, DateField

# Create your models here.
class UserProfile(models.Model):

    user=models.OneToOneField(User, on_delete=models.CASCADE)
    phone=models.CharField(max_length=20)
    DateOfBirth=DateField(max_length=50)
    profession=CharField(max_length=50)
    bloodGroup=CharField(max_length=20)
    gender=CharField(max_length=20)
    city=CharField(max_length=30)
    thana=CharField(max_length=30)
    union=CharField(max_length=30)
    postCode=CharField(max_length=20)
    otp=models.CharField(max_length=30)


    def __str__(self) -> str:
        return self.user.username

views.py

def registrations(request):

    if request.method == 'POST':
        fname = request.POST.get('fname')
        lname = request.POST.get('lname')
        username = request.POST.get('username')
        phone = request.POST.get('phone')
        email = request.POST.get('email')
        Password = request.POST.get('password')
        Password2 = request.POST.get('password2')
        DateOfBirth = request.POST.get('DateOfBirth')
        profession = request.POST.get('profession')
        bloodGroup = request.POST.get('bloodGroup')
        gender = request.POST.get('gender')
        city = request.POST.get('city')
        thana = request.POST.get('thana')
        union = request.POST.get('union')
        postCode = request.POST.get('postCode')

        check_user = User.objects.filter(email=email).first()
        check_profile = UserProfile.objects.filter(phone=phone).first()

        if Password != Password2:
            context1 = {"message1": "Password mismatch", "class1": "danger"}
            return render(request, 'UserProfile/registration.html', context1)

        if check_user or check_profile:
            context = {"message": "User already exist", "class": "danger"}
            return render(request, 'UserProfile/registration.html', context)

        user = User.objects.create_user(
            first_name=fname, last_name=lname, username=username, email=email, password=Password)
        user.save()

        profile= UserProfile(user=user,
                              phone=phone, DateOfBirth=DateOfBirth,
                              profession=profession,
                              bloodGroup=bloodGroup,
                              gender=gender,
                              city=city,
                              thana=thana,
                              union=union,
                              postCode=postCode,
        )
        profile.save()

        context = {"message": "Successfully registrations Complate",
                    "class2":"alert1 success ",
                   }
        return render(request, 'UserProfile/login.html', context)


    return render(request, 'UserProfile/registration.html')

enter image description here

I want to save the user name, email, password in the user model and others field wants to save in the UserProfile model.

Then want to fetch data like- {{user.UserProfile.phone}}

Advertisement

Answer

I had the same issue I saved the data using the same method as you did.

    if request.method =="POST":
        predata = User.objects.get(id=request.user.id)  
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        mobile_no = request.POST['mobile_no']
        email = request.POST['email']
        user_image = request.FILES['profile_image']
        
        predata.first_name = first_name
        predata.last_name = last_name
        
        predata.email = email
        messages.info(request,"Your account details successfully updated")
        predata.save()

        ext_data = Extended_user(user=predata, mobile_no = mobile_no, user_image = user_image)  
        
        ext_data.save()

And now I’m able to save the data.

For fetching the data you need to use this- {{user.userprofile.phone}}

For me the name of extended class is Extended_user and I used {{user.extended_user.mobile_no}}

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement