我想将我的自定义 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)
表格.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
视图.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})
当我提交表单时,它给出了错误密码,但用户创建表单在表单上显示了密码 1 和密码 2 的文本字段。
并告诉我为TrustAdministration模型创建用户条目的方法是对还是错。