Advertisement
mennanov

Untitled

Jul 16th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. name = ['a', 'b', 'c']
  2. for input_name in names:
  3.     field = forms.CharField(widget=DimensionsWidget(dimensions), required=False)
  4.  
  5.     def wrapper(field_name):
  6.  
  7.         def clean_dimensions(self):
  8.             """
  9.             Dimensions field clean method
  10.             """
  11.             # field_name == 'c' - ALWAYS!! :(
  12.             value = self[field_name].value()
  13.             try:
  14.                 for i in value:
  15.                     int(i)
  16.             except ValueError as e:
  17.                 raise ValidationError(self.error_messages['invalid_int'] + unicode(e))
  18.         return clean_dimensions
  19.  
  20.     # monkey-patch the clean method for this field
  21.     setattr(self, 'clean_' + input_name, types.MethodType(wrapper(input_name), self))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement