# # PseudoFieldManager v0.0 - Yeago # # Just make sure your model manager has a corresponding # snake_filter method and it shall be called when you do # filter(snake='pliskin') # # Ain't pretty right now, but its flexible.... # # YourManagerClass(PseudoFieldManager): # def language_filter(value,suffix=None): # return self.filter(Q( {'primary_language%s' % (suffix or '') : value })| # Q({'secondary_language%s % (suffix or ''): value})) # # will accurately convert filters such as.... # * filter(language='english') # * filter(language__in=['english',spanish']) # * filter(language__startswith='english') <-- __wtvr # * filter(language='aramaic',middle_name='H.') <-- pseudo + non-pseudo from django.db import models from django.core.exceptions import FieldError class PseudoFieldManager(models.Manager): def filter(self,*args,**kwargs): try: return super(PseudoFieldManager, self).filter(*args,**kwargs) except FieldError,e: # # **Dear Django, pass 'field name' and 'filter name' in exception args! # # Thanks, -yeago import re pattern = re.compile("Cannot resolve keyword '([^']+)' into field.*") matches = pattern.match(e.message) if matches and hasattr(self,'%s_filter' % matches.groups()[0]): #Great, so our manager has a foo_filter method to call. Let's rock suffix = None # May store __in, __startswith, etc. field_name = matches.groups()[0] filter_string = field_name # Might be the same. But might not. filter_pattern = re.compile('^(%s__\w+)$' % field_name) for kwarg in kwargs: kwarg_match = filter_pattern.match(kwarg) if kwarg_match: filter_string = kwarg suffix = '__%s' % filter_string.split("__")[1] # Great, so they're using foo__in=val or something. # Let's get outta here. break value = kwargs[filter_string] # Call the errant field with the corresponding foo_filter, # Also, carry on the query! Maybe they passed in other kwargs! # It will call recursively if there's another pseudo_field. del kwargs[filter_string] return getattr(self,'%s_filter' % field_name)(value,suffix=suffix).filter(*args,**kwargs) raise # Play us out. http://www.youtube.com/watch?v=2tJjNVVwRCY