Skip to content

Instantly share code, notes, and snippets.

@philipn
Created July 19, 2012 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipn/3140557 to your computer and use it in GitHub Desktop.
Save philipn/3140557 to your computer and use it in GitHub Desktop.
def filter_value_to_python(self, value, field_name, filters=None,
filter_expr=None, filter_type=None):
"""
Turn the string ``value`` into a python object.
"""
# Simple values
if value in ['true', 'True', True]:
return True
elif value in ['false', 'False', False]:
return False
elif value in ('nil', 'none', 'None', None):
return None
# Split on ',' if not empty string and either an in or range filter.
if filter_type in ('in', 'range') and len(value):
if hasattr(filters, 'getlist'):
value = []
for part in filters.getlist(filter_expr):
value.extend(part.split(','))
else:
value = value.split(',')
to_python = self.filter_value_to_python
# We just use None to ensure the to_python call is just a
# string -> python object conversion.
return [to_python(v, field_name, None, None, None) for v in value]
# Attempt to use the associated Field object to convert the value to a
# python object.
try:
value = self.fields[field_name].convert(value)
except:
pass
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment