【问题标题】:Passing choice from ChoiceField not accepting从 ChoiceField 传递选择不接受
【发布时间】:2016-02-09 18:14:26
【问题描述】:

我在使用 django 时遇到问题。出于某种愚蠢的原因,Django 想说我不能将选择作为表单的一部分传递。它固执地说

“用户”对象没有“选择”属性

这是公牛,因为我没有引用用户对象。我不知道如何解决这个问题。我只需要从表单中选择视图方法。有人知道怎么做吗?

这是表格...

''' Form used in medical personnel registration. Extends UserCreationForm '''
class MedicalPersonnelRegisterForm(UserCreationForm):
#email = EmailField(required = True)
choices = [
    (Nurse, 'Nurse'),
    (Doctor, 'Doctor'),
    (HospitalAdmin, 'Admin'),
]
position = forms.ChoiceField(choices = choices)
class Meta:
    model = User
    fields = ("username", "password1", "password2")
def clean_password2(self):
    password1 = self.cleaned_data.get("password1")
    password2 = self.cleaned_data.get("password2")
    if password1 and password2 and password1 != password2:
        raise forms.ValidationError(
            self.error_messages['password_mismatch'],
            code='password_mismatch',
        )
    return password2
def save(self, commit=True):
    user=super(UserCreationForm, self).save(commit=False)
    user.set_password(self.clean_password2())
    if commit:
        user.save()
    return user

视图看起来像这样......

''' This displays the Medical personnel login form. Unique because of radio box for type of user '''
def personnel_registration(request):
if request.method == "POST":
    form = MedicalPersonnelRegisterForm(request.POST)
    if form.is_valid():
        new_user = form.save()
        if new_user.choices == Nurse: # TODO : THIS MIGHT BREAK BADLY
            new_nurse = Nurse(user = new_user)
            new_nurse.save()
            temp = Event(activity= '\n'+new_nurse.user.get_full_name()+" has registered as a Nurse")
            temp.save()
        elif new_user.choices == Doctor:
            new_doctor = Doctor(user = new_user)
            new_doctor.save()
            temp = Event(activity= '\n'+new_doctor.user.get_full_name()+" has registered as a Doctor")
            temp.save()
        else:
            new_admin = HospitalAdmin(user = new_user)
            new_admin.save()
            temp = Event(activity= '\n'+new_admin.user.get_full_name()+" has registered as a Admin")
            temp.save()
        return HttpResponseRedirect('/login/') # TODO : Refer them to there home page

else:
    form = MedicalPersonnelRegisterForm()
return render(request, "main_site/personnel_registration.html", {
    "form" : form,
})

【问题讨论】:

  • 好吧,正如您在代码中的评论所说,它确实很糟糕。是什么让您认为 User 确实具有“选择”属性?
  • 我不知道。唯一的问题是我需要它作为表单的一部分传递来实例化一种用户。出于某种原因,它不想识别它,我不知道如何让它接受位置/选择是一件事。同样的问题发生在另一个有两个 CharField 的类中。 @丹尼尔

标签: python django forms web


【解决方案1】:

你在这里有很多混乱的地方,所以很难解开你真正想要的东西。

首先,您应该注意错误发生的位置:在视图中,而不是在表单中。在它发生的地方,你有一个 User 模型的实例。这在任何地方都没有choices 属性。

其次,即使您正在查看表单,您仍然没有任何名为 choices 的内容。您使用选项为position 字段设置允许的值和用户可读的内容,但choices 本身不是字段。

由于position是表单上的一个额外字段,与User模型无关,所以需要从form.cleaned_data中的表单数据本身获取。所以:

if form.cleaned_data['position'] == Nurse:
   ...

【讨论】:

    猜你喜欢
    • 2010-12-09
    • 2018-05-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2016-08-29
    • 1970-01-01
    相关资源
    最近更新 更多