Skip to content

Instantly share code, notes, and snippets.

@johnnncodes
Created November 2, 2013 12:38
Show Gist options
  • Save johnnncodes/7278495 to your computer and use it in GitHub Desktop.
Save johnnncodes/7278495 to your computer and use it in GitHub Desktop.
Django Forms ChoiceField Snippets
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