Как сохранить некоторые данные в пользовательской модели и некоторые данные сохранить в расширенной пользовательской модели из html-формы
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')
введите описание изображения здесь
Я хочу сохранить имя пользователя, email, пароль в модели user, а другие поля хочу сохранить в модели UserProfile.
Затем нужно получить данные типа {{user.UserProfile.phone}}