I have a choiceField
in order to create a select field with some options. Something like this:
forms.py
class NewForm(forms.Form):
title = forms.CharField(max_length=69)
parent = forms.ChoiceField(choices = CHOICE)
But I want to be able to create the options without having a predefined tuple (which is required by ChoiceField
). Basically, I need to have access to request.user to fill some options tags according to each user, but I don't know if there is any way to use request in classes of forms.Form.
An alternative would be to prepopulate the instance of NewForm
via:
views.py
form = NewForm(initial={'choices': my_actual_choices})
but I have to add a dummy CHOICE to create NewForm and my_actual_choices doesn't seem to work anyway.
I think a third way to solve this is to create a subclass of ChoiceField and redefined save()
but I'm not sure how to go about doing this.
super()
before the constructor, but when I tried it, I got an error: name 'self' is not defined.super()
has to be in the constructor. Looking at the edit history, I think that's what Surya meant, assuper()
was originally the last line in the constructor. – Biotechnology