Skip to content

Instantly share code, notes, and snippets.

@marklit
Created August 12, 2012 18:23
Show Gist options
  • Save marklit/3333563 to your computer and use it in GitHub Desktop.
Save marklit/3333563 to your computer and use it in GitHub Desktop.
def apply_filters(self, request, applicable_filters):
"""
An ORM-specific implementation of ``apply_filters``.
The default simply applies the ``applicable_filters`` as ``**kwargs``,
but should make it possible to do more advanced things.
"""
positive_filters = {}
negative_filters = {}
for lookup in applicable_filters.keys():
if lookup.endswith( '__not_eq' ):
negative_filters[ lookup ] = applicable_filters[ lookup ]
else:
positive_filters[ lookup ] = applicable_filters[ lookup ]
return self.get_object_list(request).filter(**positive_filters).exclude(**negative_filters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment