Created
November 2, 2013 12:38
-
-
Save johnnncodes/7278495 to your computer and use it in GitHub Desktop.
Django Forms ChoiceField Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchForm(SearchForm): | |
city = forms.ChoiceField(required=False) | |
province = forms.ChoiceField(required=False) | |
def __init__(self, *args, **kwargs): | |
# DEMO: to get list of fields w/ changed data | |
# example: check if city choice data has changed | |
if 'city' in self.changed_data: | |
self.fields['city'].widget.attrs['class'] = 'show' | |
# DEMO: to get the value of a choice field | |
# example: get the value of city choice field | |
for name, field in self.fields.items(): | |
if name is 'city': | |
prefixed_name = self.add_prefix(name) | |
data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name) | |
print data_value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment