1

我在执行项目时收到此错误

/accounts/EmpReg/的 FieldError
无法将关键字“用户名”解析为字段。选项有:companytype、contactno、contactperson、id、jobs、user

视图.py

class User(models.Model):
    username = models.CharField(_('username'), max_length=30, unique=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    email = models.EmailField(_('e-mail address'), blank=True)
    password = models.CharField(_('password'), max_length=128)
    companyname = models.CharField(_('companyname'), max_length=50)
    usertype = models.CharField(_('usertype'), max_length=30)
    is_staff = models.BooleanField(_('staff status'), default=False)
    is_active = models.BooleanField(_('active'), default=True)
    is_superuser = models.BooleanField(_('superuser status'), default=False)
    last_login = models.DateTimeField(_('last login'), default=timezone.now)
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
    groups = models.ManyToManyField(Group, verbose_name=_('groups'),blank=True)
    user_permissions = models.ManyToManyField(Permission,verbose_name=_('user permissions'), blank=True)


class RegistrationProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    activation_key = models.CharField(max_length=40)
    key_generated = models.DateTimeField()
    objects = RegistrationManager()

表格.py

class EmpRegistrationForm(forms.Form):
    username = forms.CharField(max_length=30,widget=forms.TextInput)
    email = forms.EmailField(widget=forms.TextInput))
    password1 = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(widget=forms.PasswordInput)
    usertype = forms.CharField(widget=forms.HiddenInput)
    companyname = forms.CharField(widget=forms.TextInput)
    companytype = forms.ChoiceField(choices=PREFERRED_COMPANYTYPE, widget=forms.RadioSelect())
    contactno = forms.CharField(widget=forms.TextInput)
    contactperson = forms.CharField(widget=forms.TextInput)
    tos = forms.BooleanField()

视图.py

def empreg(request):
    if request.method == 'POST':
        form = EmpRegistrationForm(request.POST)
        if form.is_valid():
            user = RegistrationProfile.objects.create_inactive_user(user__username=form.cleaned_data['username'],password=form.cleaned_data['password1'],email=form.cleaned_data['email'],)
            user.companyname = form.cleaned_data['companyname']
            user.usertype = form.cleaned_data['usertype']
            user.save()
            e=EmployerReg_Form(user=user, companytype=form.cleaned_data['companytype'],contactno=form.cleaned_data['contactno'],contactperson=form.cleaned_data['contactperson'])
            e.save()
            return HttpResponseRedirect('/accounts/EmpReg_Complete/')
else:
    form = EmpRegistrationForm()
    return render_to_response('registration/empregform.html', { 'form': form }, context_instance=RequestContext(request))

模型.py

class EmployerReg_Form(models.Model):

    user = models.ForeignKey(User, unique=True)
    companytype = models.CharField(max_length=20)
    contactno = models.CharField(max_length=30)
    contactperson = models.CharField(max_length=30)

PREFERRED_COMPANYTYPE = (
('Company', 'Company'),
('Consultancy', 'Consultancy'),
)

请检查上面的代码并在我出错的地方提供帮助

4

1 回答 1

1

在这一部分中,您将调用 EmployerReg_Form:

e=EmployerReg_Form(
         user=user,
         companytype=form.cleaned_data['companytype'],
         contactno=form.cleaned_data['contactno'],
         contactperson=form.cleaned_data['contactperson']
  )

当您查看模型时,您会看到用户字段是用于建模用户的外键。如果您知道 ID EmployerReg_Form 应该映射到 User 您的行将是:

e=EmployerReg_Form(
         user__ID=user, 
         companytype=form.cleaned_data['companytype'],
         contactno=form.cleaned_data['contactno'],
         contactperson=form.cleaned_data['contactperson']
  )

映射到名称将是这样的:

e=EmployerReg_Form(
         user__username=user, 
         companytype=form.cleaned_data['companytype'],
         contactno=form.cleaned_data['contactno'],
         contactperson=form.cleaned_data['contactperson']
  )

注意用户后面的双下划线。

哦,用户模型不应该在你的models.py而不是views.py中吗?

于 2013-04-17T07:08:34.833 回答