Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically remove a choice option from a form

Tags:

django

I have a form like this:

RANGE_CHOICES = (
    ('last', 'Last Year'),
    ('this', 'This Year'),
    ('next', 'Next Year'),
)   

class MonthlyTotalsForm(forms.Form):
    range = forms.ChoiceField(choices=RANGE_CHOICES, initial='this')

It's displayed in the template like this:

{{ form.range }}

In some situations I don't want to show the 'Next Year' option. Is it possible to remove this option in the view where the form is created?

like image 330
gerty Avatar asked May 31 '11 14:05

gerty


People also ask

How do I make the choices disappear in Google Forms?

Click on the “Add-ons” puzzle piece icon. Select “Choice Eliminator 2” and then “Configure.” In the mini box to the right, click on your first question. Then select “Eliminate Choices.” It will take a few seconds to process.

Can you make a Google Form dynamic?

Dynamic Fields Add-on for Google Forms™ populates values of selection fields. Questions of type Multiple-choice, Drop-down, Checkbox or Grid can be updated by data from Sheets, Contacts or Groups. Create in Google Forms™ dynamic choice boxes.

How do I get choice Eliminator 2?

Create a new Google Form and then click on the three vertical dots to the right-hand side of the 'Send' button. In here click on 'Add-ons' and in the search apps box type in Choice Eliminator. There are two options to pick from. Choice Eliminator 2 and Choice Eliminator Lite.

How do I delete an answer key in Google Forms?

Open your form in Google Forms > Click Responses > Click Individual > Click previous or next icon to view the response you want to delete > Click delete icon > Confirmation popup will be displayed.


1 Answers

class MonthlyTotalsForm(forms.Form):
    range = forms.ChoiceField(choices=RANGE_CHOICES, initial='this')

    def __init__(self, *args, **kwargs):
        no_next_year = kwargs.pop('no_next_year', False)
        super(MonthlyTotalsForm, self).__init__(*args, **kwargs)
        if no_next_year:
            self.fields['range'].choices = RANGE_CHOICES[:-1]

#views.py
MonthlyTotalsForm(request.POST, no_next_year=True)
like image 133
DrTyrsa Avatar answered Sep 30 '22 13:09

DrTyrsa