Skip to content

Instantly share code, notes, and snippets.

@Nearhan
Last active December 16, 2015 14:09
Show Gist options
  • Save Nearhan/5446779 to your computer and use it in GitHub Desktop.
Save Nearhan/5446779 to your computer and use it in GitHub Desktop.
Combing Two Mixins
class CityAndStateInputMixin(object):
def __init__(self, *args, **kwargs):
super(CityAndStateInputMixin, self).__init__(*args, **kwargs)
#add custom error_class
self.error_class = CustomListError
#adding State Selct Widget and dynamic css edits
self.fields['state'] = forms.ChoiceField(choices=US_STATES,
widget=StateSelectWidget)
self.fields['state'].widget.attrs = {'class': 'custom state',
'placeholder': 'State'}
#adding custom_city
self.fields['city'].widget.attrs.update({'class': 'city'})
class AddPlaceHolderMixin(object):
def __init__(self, *args, **kwargs):
super(AddPlaceHolderMixin, self).__init__(*args, **kwargs)
self.add_placeholder_class()
def add_placeholder_class(self):
for key, field in self.fields.items():
if key in self.required_fields:
try:
field.widget.attrs.update(
{'placeholder': '{0} (required)'.format(field.label)})
except KeyError:
pass
else:
try:
field.widget.attrs.update(
{'placeholder': '{0}'.format(field.label)})
except KeyError:
pass
class MyCustomForm(CityAndStateInputMixin, AddPlaceHolderMixin):
def __init__(self, *args, **kwargs):
return super(MyCustomForm, self).__init__(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment