Login

PseudoFieldManager

Author:
yeago
Posted:
April 9, 2009
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

First version Apr 9 09

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#
# 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

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.