model.py
JavaScript
x
29
29
1
from django.db import models
2
3
from django.contrib.auth.models import User
4
5
from django.db.models.signals import post_save
6
7
from django.dispatch import receiver
8
9
from django.db.models.fields import CharField, DateField
10
11
# Create your models here.
12
class UserProfile(models.Model):
13
14
user=models.OneToOneField(User, on_delete=models.CASCADE)
15
phone=models.CharField(max_length=20)
16
DateOfBirth=DateField(max_length=50)
17
profession=CharField(max_length=50)
18
bloodGroup=CharField(max_length=20)
19
gender=CharField(max_length=20)
20
city=CharField(max_length=30)
21
thana=CharField(max_length=30)
22
union=CharField(max_length=30)
23
postCode=CharField(max_length=20)
24
otp=models.CharField(max_length=30)
25
26
27
def __str__(self) -> str:
28
return self.user.username
29
views.py
JavaScript
1
54
54
1
def registrations(request):
2
3
if request.method == 'POST':
4
fname = request.POST.get('fname')
5
lname = request.POST.get('lname')
6
username = request.POST.get('username')
7
phone = request.POST.get('phone')
8
email = request.POST.get('email')
9
Password = request.POST.get('password')
10
Password2 = request.POST.get('password2')
11
DateOfBirth = request.POST.get('DateOfBirth')
12
profession = request.POST.get('profession')
13
bloodGroup = request.POST.get('bloodGroup')
14
gender = request.POST.get('gender')
15
city = request.POST.get('city')
16
thana = request.POST.get('thana')
17
union = request.POST.get('union')
18
postCode = request.POST.get('postCode')
19
20
check_user = User.objects.filter(email=email).first()
21
check_profile = UserProfile.objects.filter(phone=phone).first()
22
23
if Password != Password2:
24
context1 = {"message1": "Password mismatch", "class1": "danger"}
25
return render(request, 'UserProfile/registration.html', context1)
26
27
if check_user or check_profile:
28
context = {"message": "User already exist", "class": "danger"}
29
return render(request, 'UserProfile/registration.html', context)
30
31
user = User.objects.create_user(
32
first_name=fname, last_name=lname, username=username, email=email, password=Password)
33
user.save()
34
35
profile= UserProfile(user=user,
36
phone=phone, DateOfBirth=DateOfBirth,
37
profession=profession,
38
bloodGroup=bloodGroup,
39
gender=gender,
40
city=city,
41
thana=thana,
42
union=union,
43
postCode=postCode,
44
)
45
profile.save()
46
47
context = {"message": "Successfully registrations Complate",
48
"class2":"alert1 success ",
49
}
50
return render(request, 'UserProfile/login.html', context)
51
52
53
return render(request, 'UserProfile/registration.html')
54
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.
JavaScript
1
19
19
1
if request.method =="POST":
2
predata = User.objects.get(id=request.user.id)
3
first_name = request.POST['first_name']
4
last_name = request.POST['last_name']
5
mobile_no = request.POST['mobile_no']
6
email = request.POST['email']
7
user_image = request.FILES['profile_image']
8
9
predata.first_name = first_name
10
predata.last_name = last_name
11
12
predata.email = email
13
messages.info(request,"Your account details successfully updated")
14
predata.save()
15
16
ext_data = Extended_user(user=predata, mobile_no = mobile_no, user_image = user_image)
17
18
ext_data.save()
19
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}}