我正在尝试创建一个使用Django的注册用户页面,并且在添加用户时,遇到了一个问题,该问题一直按照本文标题返回。我在下面发布了一些作品。如果有人可以帮助我并告诉我哪里出了问题,我将不胜感激。
this is my views.py
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
pwd = make_password(form.cleaned_data.get('password1'))
mail = form.cleaned_data.get('userEmail')
fname = form.cleaned_data.get('firstname')
lname = form.cleaned_data.get('lastname')
contact = form.cleaned_data.get('userContact')
company = form.cleaned_data['company']
clearance = form.cleaned_data['clearance']
args = (username, pwd, mail,fname, lname, contact, company, clearance)
db = MySQLdb.connect(host=var.host, user=var.user, password=var.password, db=var.db)
cur = db.cursor()
cur.callproc('sp_NewSignUp', args)
db.commit()
cur.close()
db.close()
messages.success(request, f'Account created for {username}!')
return redirect('homepage')
else:
print("form error")
else:
form = UserRegisterForm()
return render(request, 'main/register.html', {'form': form})
`````forms.py``````
db = MySQLdb.connect(host=var.host, user=var.user, password=var.password, db=var.db)
cur = db.cursor()
cur.callproc('sp_getClearanceRegister', args='')
account_choice = []
for i in cur.fetchall():
account_choice.append(i)
cur.nextset()
user = 'root'
companyid = '1'
args = user, companyid
cur.callproc('sp_getUserCompany', args)
company_choice = []
for i in cur.fetchall():
company_choice.append(i)
cur.close()
db.close()
class UserRegisterForm(UserCreationForm):
userEmail = forms.EmailField(label='Email')
firstname = forms.CharField(label='First Name')
lastname = forms.CharField(label='Last Name')
userContact = forms.IntegerField(label='Contact No.')
clearance = forms.CharField(label='Clearance', widget=forms.Select(choices=account_choice))
company = forms.CharField(label='Company', widget=forms.Select(choices=company_choice))
class Meta:
model = get_user_model()
fields = ['username', 'password1', 'password2','userEmail', 'firstname','lastname', 'userContact', 'clearance', 'company']
models.py
class company(models.Model):
company_id = models.AutoField('Company ID', primary_key = True)
company_name = models.CharField('Company Name',max_length=255, unique=True)
company_address = models.CharField('Company Address', max_length=255)
company_email = models.CharField('Company Email', max_length=255)
company_contact = models.CharField('Company Contact Info', max_length=45)
date_of_creation = models.DateTimeField('Date Company Joined')
date_of_lastModified = models.DateTimeField('Date Of Company Info Last Modified')
class Meta:
verbose_name_plural = "company"
def __str__(self):
return self.company_name
class clearance(models.Model):
clearance_id = models.AutoField('Clearance ID', primary_key = True)
clearance_name = models.CharField('Clearance Name', max_length=45, unique=True)
clearance_level = models.IntegerField('Clearance Level')
date_of_creation = models.DateTimeField('Date Clearance Added')
date_of_last_modified = models.DateTimeField('Date Of Clearance Last Modified')
class Meta:
verbose_name_plural = "clearance"
def __str__(self):
return self.clearance_name
class user(AbstractUser):
user_id = models.AutoField('User ID', primary_key = True)
user_contact = models.CharField('Contact', max_length=45)
clearance = models.ForeignKey(clearance, default = 1, verbose_name = "Clearance ID", on_delete = models.SET_DEFAULT)
company = models.ForeignKey(company, default = 1, verbose_name = "Company ID", on_delete = models.SET_DEFAULT)
#objects = UserManager()
class Meta:
verbose_name_plural = "user"
def __str__(self):
return self.username
最初,添加用户直到更改为abstractuser都没问题。并且我收到此错误消息,指出
无法分配“'5'”:“ user.clearance”必须是“ clearance”实例。”
我觉得符号“ 5”存在一些问题,但我不知道如何解决。