Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom Filter in Django Admin returns SuspiciousOperation
    primarykey
    data
    text
    <p>I am trying to implement a new version of <a href="http://djangosnippets.org/snippets/2229/" rel="nofollow">this snippet</a>, making it compatible for Django 1.4 and 1.5</p> <p>Quite a bit has evolved, the code below almost working excepts that Django returns me a SuspiciousOperation error. I see how I could hack it, but I would prefer not to touch the core of Django. If you have some suggestions, there are welcome :</p> <p>Here is my filter :</p> <pre><code>class RelatedNullFilterSpec(FieldListFilter): def __init__(self, field, request, params, model, model_admin, field_path): field_root, field_name = field_path.rsplit('__', 1) self.lookup_title = field.verbose_name self.title = self.lookup_title self.null_lookup_kwarg = '%s__isnull' % field_root self.null_lookup_val = request.GET.get(self.null_lookup_kwarg, None) self.lookup_kwarg = '%s__exact' % (field_path) self.lookup_val = request.GET.get(self.lookup_kwarg, None) if isinstance(field, models.fields.BooleanField): self.lookup_choices = ( # (None, _('All')), ('1', _('Yes')), ('0', _('No'))) else: self.lookup_choices = field.get_choices(include_blank=False) super(RelatedNullFilterSpec, self).__init__(field, request, params, model, model_admin, field_path) def expected_parameters(self): return [self.lookup_kwarg, self.null_lookup_kwarg] def choices(self, cl): yield {'selected': self.lookup_val is None and self.null_lookup_val is None, 'query_string': cl.get_query_string({}, [self.lookup_kwarg,self.null_lookup_kwarg]), 'display': _('All')} yield {'selected': self.lookup_val is None and self.null_lookup_val=="True", 'query_string': cl.get_query_string({self.null_lookup_kwarg:True},[self.lookup_kwarg]), 'display': _('Null')} yield {'selected': self.lookup_val is None and self.null_lookup_val=="False", 'query_string': cl.get_query_string({self.null_lookup_kwarg:False},[self.lookup_kwarg]), 'display': _('Not Null')} for pk_val, val in self.lookup_choices: yield {'selected': self.lookup_val == smart_unicode(pk_val), 'query_string': cl.get_query_string({self.lookup_kwarg: pk_val},[self.null_lookup_kwarg]), 'display': val} </code></pre> <p>Then in my admin, I have the following:</p> <pre><code>list_filter = ('time_added', 'time_modified', ('model1__model2__property', RelatedNullFilterSpec),) </code></pre> <p>And I always got this error from the <code>lookup_allowed</code> method of the Django BaseModelAdmin class...</p> <p>In django.db.models.options, I could implement a hack to overwrite or extend the <code>self.related_fkey_lookups</code> but it is a bit too hacky to my taste.</p> <p>EDIT: Notice that the following almost standard filter also returns the same error: <code>('venue__eat_venue', BooleanFieldListFilter)</code></p> <p>In general, my aim is that I would like a filter that allows me to sort the objects by presence/absence of the model2 related field (Null/Not-Null) and by the values of the property (in case the model2 related field exists). It would be quite handy and I don't think too specific.</p> <p>And finally, yes, everything works when I do not request this custom filter for my <code>model1__model2__property</code> :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload