Django额外的用户注册详细信息未保存到数据库 [英] Django extra user registration details not saving to database
问题描述
我一直在尝试扩展django用户个人资料,但是它不会将额外的详细信息保存到数据库中,这是企业,学生,游客的user_type类型,我不知道为什么.任何帮助,将不胜感激.
I have been trying to extend the django user profile but it won't save the extra details to the database which is a user_type of business, student, tourist and I can't figure out why. Any help would be appreciated.
它不会在数据库中保存为空,而是保存为空白.
It doesn't save in the DB as null, it is saving as just blank.
使用python 3和django的最新版本.
Using python 3 with the latest version of django.
Forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.forms import ModelChoiceField
from django.contrib.auth.models import User, Group
from .models import UserProfile
class RegisterForm(UserCreationForm):
first_name = forms.CharField(max_length=50, required=False,
help_text='Not required.')
last_name = forms.CharField(max_length=50, required=False,
help_text='Not required.')
email = forms.EmailField(max_length=300, required=True,
help_text='Please input a valid email address.')
user_types = ((1, 'Business'), (2, 'Student'), (3, 'Tourist'))
user_type = forms.ChoiceField(choices = user_types)
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'user_type', 'password1', 'password2',)
models.py
models.py
from django.db import models
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.db.models.signals import post_save
from django import forms
ADMIN = 0
BUSINESS = 1
STUDENT = 2
TOURIST = 3
class UserProfile(models.Model):
user_choices = ( (0, 'Admin'),
(1, 'Business'),
(2, 'Student'),
(3, 'Tourist'),
)
# Link UserProfile to User model
user = models.OneToOneField(User, on_delete=models.CASCADE)
# Add attributes to the user model
user_type = models.CharField(max_length = 50,
choices = user_choices,
# default = 'Business',
blank = False)
def __str__(self):
return self.user.username
@receiver(post_save, sender = User)
def create_user_profile( sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user = instance)
instance.userprofile.save()
Views.py
def register(request):
if request.method == 'POST':
form = RegisterForm(data=request.POST)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
# form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user_group = form.cleaned_data.get('user_type')
user = authenticate(username = username, password = password)
login(request, user)
if user_group == 'Business':
return render(request, 'users/business.html')
elif user_group == 'Student':
return render(request, 'users/student.html')
elif user_group == 'Tourist':
return render(request, 'users/tourist.html')
else:
return redirect('main:index')
else:
print(RegisterForm.errors)
else:
form = RegisterForm()
return render(request, 'users/register.html', {'form': form})
# return HttpResponseRedirect(reverse('main:index'))
def business_view(request):
return render(request, 'users/business.html')
def student_view(request):
return render(request, 'users/student.html')
def tourist_view(request):
return render(request, 'users/tourist.html')
推荐答案
您的表单元模型是 User
,因此,当您尝试:
Your form meta model is User
, so when you try:
profile = form.save(commit=False)
变量 profile
是 User
类的实例,因此,由于具有创建 UserProfile
实例的信号,因此应使用此代码:
the variable profile
is an instance of the User
class, so, since you have the signal that creates the UserProfile
instance, you should use this code:
user = form.save()
profile = user.userprofile
user_group = form.cleaned_data.get('user_type')
profile.user_type = user_group
profile.save()
代替:
profile = form.save(commit=False)
profile.user = request.user
profile.save()
这篇关于Django额外的用户注册详细信息未保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!