Django字段错误无法将关键字“用户名”解析为字段。选择包括:companytype,contactno,contactperson,id,jobs,user

时间:2013-04-17 05:22:49

标签: django

我执行项目时收到此错误

  

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

views.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()

forms.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()

views.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))

models.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'),
)

请检查上面的代码并帮助我做错了

1 个答案:

答案 0 :(得分: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?