Python 我可以制作一个包含不同字段和注册字段的表单吗?
models.pyPython 我可以制作一个包含不同字段和注册字段的表单吗?,python,django,Python,Django,models.py class employeeModel(models.Model): employee_id = models.CharField(max_length=300,unique=True,help_text='Employee ID should not be same') name = models.CharField(max_length=300) father_name = models.CharField(max_length=300)
class employeeModel(models.Model):
employee_id = models.CharField(max_length=300,unique=True,help_text='Employee ID should not be same')
name = models.CharField(max_length=300)
father_name = models.CharField(max_length=300)
username = models.CharField(max_length=50,null=True,blank=True)
email = models.EmailField(null=True,blank=True)
password = models.CharField(max_length=20,null=True,blank=True)
def get_absolute_url(self):
return reverse('detail_view',kwargs={'employee_id':self.employee_id})
def __str__(self):
return self.employee_id
class Meta:
verbose_name = 'Employee Detail'
views.py
User = get_user_model()
def create_view(request):
if request.method == 'POST':
form = employeeForm(request.POST or None,request.FILES or None)
if form.is_valid():
username = form.cleaned_data['username']
email = form.cleaned_data['email']
password = form.cleaned_data['password']
User= get_user_model()
user=
User.objects.create_user(
username=username,email=email,password=password
)
authenticate(
request,username=username,email=email,password=password
)
user.save()
form.save()
return redirect('emp_list')
else:
form = employeeForm()
return render(request,'create_employee.html',{'form':form})
虽然不会引发任何错误,但用户无法通过此方法创建实例,尽管EmployeeModel.objects.all()中显示的是“用户对象”,而不是User.objects.all()中的。这就是为什么当我创建任何用户时,尽管此表单登录失败,因为它们不是用户的对象
我能做些什么使它工作?或者我必须放弃此代码?models.py
class employeeModel(models.Model):
def __init__(self, employee_id, name, father_name, username, email, password):
self.employee_id = models.CharField(max_length=300,unique=True,help_text='Employee ID should not be same')
self.name = models.CharField(max_length=300)
self.father_name = models.CharField(max_length=300)
self.username = models.CharField(max_length=50,null=True,blank=True)
self.email = models.EmailField(null=True,blank=True)
self.password = models.CharField(max_length=20,null=True,blank=True)
def __repr__(self):
return "Employee: {}".format(self.name)
我的建议是:如上所述,对employee类尝试init方法。models.py
class employeeModel(models.Model):
def __init__(self, employee_id, name, father_name, username, email, password):
self.employee_id = models.CharField(max_length=300,unique=True,help_text='Employee ID should not be same')
self.name = models.CharField(max_length=300)
self.father_name = models.CharField(max_length=300)
self.username = models.CharField(max_length=50,null=True,blank=True)
self.email = models.EmailField(null=True,blank=True)
self.password = models.CharField(max_length=20,null=True,blank=True)
def __repr__(self):
return "Employee: {}".format(self.name)
我的建议是:如上所述,尝试employee类的init方法。@chidandanadanayak,是的,尽管不确定如何使用employeeModel类创建用户,但请确保类中传递的所有参数都与init方法中定义的参数数相等 榜样
id = 123
name = 'John Doe'
father_name = 'Patrick Doe'
username = 'john_doe'
email = 'john_doe@demo.com'
password = '1234567890'
new_user = employeeModel(id, name, father_name, email, password)
@ChidanandaNayak,是的,尽管不确定您打算如何使用employeeModel类来创建用户,但请确保类中传递的所有参数都与init方法中定义的参数数量相等 榜样
id = 123
name = 'John Doe'
father_name = 'Patrick Doe'
username = 'john_doe'
email = 'john_doe@demo.com'
password = '1234567890'
new_user = employeeModel(id, name, father_name, email, password)
@ZackAtma那么,我可以在表单中覆盖这些字段吗?像占位符或任何其他属性一样?@ZackAtma那么,我可以在表单中覆盖这些字段吗?就像占位符或其他属性一样?但问题出现了,所以新用户是EmployeeModel的实例而不是用户模型?这就是我登录失败的原因。是的,但是你的用户模型或get\u User\u model()方法和create\u User()方法在哪里?问题可能出在这些方法中,但是问题出现了,所以新的用户是EmployeeModel的实例,而不是用户模型?这就是我登录失败的原因。是的,但是你的用户模型或get\u User\u model()方法和create\u User()方法在哪里?问题可能在于这些方法。