【问题标题】:Understanding UpdateView to change the Form and FormWidget了解UpdateView改变Form和FormWidget
【发布时间】:2015-04-16 21:12:35
【问题描述】:

我还是不明白 UpdateView。它从哪里获取表格? 它具有模型中声明的字段,但不使用 forms.py 中定义的表单。

但是我确实遵循了以下给出的答案:

How does one use a custom widget with a generic UpdateView without having to redefine the entire form?

在我的例子中,我在 model.py 中使用 IntegerField 并在 Form 中使用 Radiobuttons。

所以 UpdateView 的作用是给我一个 IntegerField 而不是 ChoiceField。即使我分配了 RadioSelect 小部件或选择字段:

观点:

class UpdateEinflussideen(UpdateView):

    model = Einflussideen
    EINFLUSS = [(10,'hoch'),(4,'mittel'),(1,'gering')]
    form_class = forms.models.modelform_factory(Einflussideen,
                    widgets={'einfluss': forms.ChoiceField(
                        choices=EINFLUSS, widget=forms.RadioSelect())},
                    )
    template_name = 'verbrauchererfassung/update_einflussideen.html'
    success_url = reverse_lazy('verbraucher')

模型:

class Einflussideen(models.Model):

    idee = models.CharField(max_length=100)
    einfluss = models.IntegerField()
    verbraucher = models.ForeignKey(Verbraucher)

【问题讨论】:

标签: python django django-class-based-views


【解决方案1】:

python 中的变量区分大小写。将属性form_Class 更改为form_class。此外,widgets 参数应该包含一个带有 Widget 实例的字典:

form_class = forms.models.modelform_factory(Einflussideen,
                 widgets={'einfluss': forms.RadioSelect(choices=EINFLUSS)})

【讨论】:

  • 好的——这有效并引导我进入“ChoiceField”对象没有属性“needs_multipart_form”——我认为这是因为 ChoiceField 是一个字段而不是一个小部件。只使用 RadioSelect 给我的只是标签
  • 您应该将choices 参数传递给小部件构造函数。查看更新的答案。
  • 谢谢,这行得通 - 我要去散步以理清头绪 - 错别字;)
猜你喜欢
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2011-04-07
相关资源
最近更新 更多