我想将自定义TrustAdministration字段添加到django中的Auth_User表
这是我的 Model.py
from django.db import models
from django.contrib.auth.models import User
class Trust(models.Model):
name = models.CharField(max_length=200)
class TrustAdministration(models.Model):
user = models.OneToOneField(User)
full_name = models.CharField(max_length=200)
email = moels.CharField(max_length=75)
form.py
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from models import Trust, TrustAdministration
class TrustForm(form.ModelForm):
class Meta():
model = Trust
class TrustAdministration(forms.ModelForm):
class Meta():
model = TrustAdministration
class UserCreateForm(UserCreationForm):
class Meta():
model = User
view.py
def trust_registration(request):
if request.POST:
trust_form = TrustForm(request.POST, instance=Trust())
trust_admin_form = TrustAdministrationForm(request.POST,instance=TrustAdministration())
user_form = UserCreateForm(request.POST)
if trust_form.is_valid() and trust_admin_form.is_valid() and user_form.is_valid():
username = request.POST['username']
password = request.POST['password']
new_user = User.objects.create_user(username=username, password=password)
new_user.save()
new_trust = trust_form.save()
new_trust_admin = trust_admin_form.save(commit = False)
new_trust_admin.trust = new_trust
new_trust_admin.user = new_user
new_trust_admin.email = username
new_trust_admin.save()
else:
return HttpResponseRedirect('/registration/trust_registration')
return HttpResponseRedirect('/registration/trust_details')
else:
trust_form = TrustForm()
trust_admin_form = TrustAdministrationForm()
user_form = UserCreateForm()
return render(request, 'trust/trust_registration.html', {'trust_form': trust_form,
'trust_admin_form' : trust_admin_form,
'user_form' : user_form})
当我提交表单时,它会显示错误密码,但 Usercreation表单会在表单上显示密码1和密码2的文本字段。
并告诉我为 TrustAdministration 模型创建用户条目是对或错的。
答案 0 :(得分:0)
你有:
email = moels.CharField(max_length=75)
看起来你只是错误拼写的模型:
email = models.CharField(max_length=75)