#5833: Custom FilterSpecs
-------------------------------------------+--------------------------------
          Reporter:  Honza_Kral            |         Owner:  jkocherhans        
                                     
            Status:  assigned              |     Milestone:                     
                                     
         Component:  django.contrib.admin  |       Version:  SVN                
                                     
        Resolution:                        |      Keywords:  nfa-someday 
list_filter filterspec nfa-changelist ep2008
             Stage:  Accepted              |     Has_patch:  1                  
                                     
        Needs_docs:  1                     |   Needs_tests:  1                  
                                     
Needs_better_patch:  0                     |  
-------------------------------------------+--------------------------------
Comment (by bendavis78):

 @subsume, I see what you're getting at.  I still, however, think that
 yours is a rare use-case.  In the case of the Comapnies/Locations example,
 I think it would make more sense to just have a !LocationAdmin to filter
 on locations.  While I still think most developers would want the default
 behavior to work like a normal queryset chain,  I can see the benefit of
 having the option open,  so as a compromise I propose we move the querset
 processing code out into a different function so that the default behavior
 can be overidden:

 (from django/contrib/admin/views/main.py, near line 178)
 {{{
 #!python

     def apply_filter_specs(self, qs, lookup_params):
         # Let every filter spec modify the qs and params to its liking
         for filter_spec in self.filter_specs:
             new_qs = filter_spec.get_query_set(self, qs)
             if new_qs is not None and new_qs is not False:
                 qs = new_qs
                 # Only consume params if we got a new queryset
                 for param in filter_spec.consumed_params():
                     try:
                         del lookup_params[param]
                     except KeyError:
                         pass
         return qs

     def get_query_set(self):
         qs = self.root_query_set
         lookup_params = self.params.copy() # a dictionary of the query
 string
         for i in (ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR,
 IS_POPUP_VAR):
             if i in lookup_params:
                 del lookup_params[i]
         key = ''
         for key, value in lookup_params.items():
             if not isinstance(key, str):
                 # 'key' will be used as a keyword argument later, so
 Python
                 # requires it to be a string.
                 del lookup_params[key]
                 lookup_params[smart_str(key)] = value

             # if key ends with __in, split parameter into separate values
             if key.endswith('__in'):
                 lookup_params[key] = value.split(',')

             # if key ends with __isnull, special case '' and false
             if key.endswith('__isnull'):
                 if value.lower() in ('', 'false'):
                     lookup_params[key] = False
                 else:
                     lookup_params[key] = True

         qs = self.apply_filter_specs(qs, lookup_params)

         # Apply lookup parameters from the query string.
 }}}

 In your case, you would be able to override apply_filter_specs in to
 change that behavior.  By no means am i the authority on this ticket, so
 if anyone else can chime with their opinion please do.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/5833#comment:94>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to