0

I still didn't understood the UpdateView. Where does it fetch the form from? It has exactly the fields declared in the model, but doesn't use the form defined in forms.py.

I did however follow the answer given at:

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

In my case I use an IntegerField in model.py and use Radiobuttons in the Form.

So what the UpdateView does is giving me an IntegerField instead of a ChoiceField. Even when I assigned the RadioSelect Widget or a Choice Field:

The View:

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')

The Model:

class Einflussideen(models.Model):

    idee = models.CharField(max_length=100)
    einfluss = models.IntegerField()
    verbraucher = models.ForeignKey(Verbraucher)
Community
  • 1
  • 1
Maximilian Kindshofer
  • 2,753
  • 3
  • 22
  • 37

1 Answers1

1

Variables in python are case-sensitive. Change the atribute form_Class to the form_class. Also the widgets argument should contain a dict with the Widget instances in the values:

form_class = forms.models.modelform_factory(Einflussideen,
                 widgets={'einfluss': forms.RadioSelect(choices=EINFLUSS)})
catavaran
  • 44,703
  • 8
  • 98
  • 85