使用注册表添加到自定义用户字段(django)
我正在django中为注册表单创建自定义用户模型,但在将数据导入数据库时遇到了一些问题。目前,用户可以注册并保存用户名、密码、电子邮件、名字和姓氏,但所有其他表单数据都不会保存到对象中。以下是我的表格和输出: Models.py使用注册表添加到自定义用户字段(django),django,model,field,registration,Django,Model,Field,Registration,我正在django中为注册表单创建自定义用户模型,但在将数据导入数据库时遇到了一些问题。目前,用户可以注册并保存用户名、密码、电子邮件、名字和姓氏,但所有其他表单数据都不会保存到对象中。以下是我的表格和输出: Models.py class Profile(models.Model): user = models.OneToOneField(User) gender = models.CharField(max_length=20) medication = models
class Profile(models.Model):
user = models.OneToOneField(User)
gender = models.CharField(max_length=20)
medication = models.CharField(max_length=50, blank=True)
medical_history = models.CharField(max_length=50,blank=True)
DOB = models.CharField(max_length=20)
telephone = models.CharField(max_length=20)
address = models.CharField(max_length=30)
city = models.CharField(max_length=20)
state = models.CharField(max_length=20)
postcode = models.CharField(max_length=30)
forms.py:
class CreateAccountForm(ModelForm):
class Meta:
model = Profile
fields = ("username","password","password2","first_name","last_name",
"gender","medication","medical_history","DOB","email","telephone",
"address","city","state","postcode")
views.py:
def create_account(request):
form = CreateAccountForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
password2 = form.cleaned_data['password2']
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
gender = form.cleaned_data['gender']
medication = form.cleaned_data['medication']
medical_history = form.cleaned_data['medical_history']
DOB = form.cleaned_data['DOB']
email = form.cleaned_data['email']
telephone = form.cleaned_data['telephone']
address = form.cleaned_data['address']
city = form.cleaned_data['city']
state = form.cleaned_data['state']
postcode = form.cleaned_data['postcode']
if password == password2:
if (password_verification(password)) == 3:
if (username_verification(username)) == False:
form.save()
return HttpResponseRedirect('/login')
else:
return HttpResponseRedirect('/create_account')
else:
return HttpResponseRedirect('/create_account')
else:
return HttpResponseRedirect('/create_account')
return render(request, "create_account.html", {'form': form})
在django admin窗口中,用户使用create_用户字段注册到数据库,但添加的自定义字段都没有保存在可用列中。任何帮助都会很好这真的让我很烦。下面是一张空旷田野的照片,干杯 您只保存用户
User.save()
因此其他Profile
(请将User
表更改为有意义的Profile
)
此外,您的个人资料中也不需要密码名字姓电子邮件
我建议使用ModelForm
保存配置文件表
user_form = CreateAccountForm(request.POST)
profile_form = ProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
# save the form after your password check
user_form.save()
profile_form.save()
我已将模型名称更改为Profile,感谢您的建议。will form.save()进入视图文件?所以在models.py中我不需要密码、电子邮件等。用户名字段呢?同样在forms.py的ModelForm字段中,我还会包括用户名、密码、电子邮件等吗?请您检查Django用户…默认情况下,Django用户模型在配置文件
表中有您不需要的字段。对吗?我有u用编辑过的视图、模型和表单更新了我的帖子。看起来准确吗?