English 中文(简体)
Negative custom Django admin FilterSpec
原标题:

I m working on a custom Django Admin FilterSpec (covered already on SO #991926). My FilterSpec is a replacement for the default filter on ForeignKey(User), and basically replaces the list of all users with three only choices, all, mine, and others.

For example, if I applied the custom filterspec to the field created_by it would add an admin filter with All, Created by Me, and Created by Others. Everything works except the negative filter, Created by Others.

I ve been attempting to achieve this by appending __not to the query as so:

def choices(self, cl):
    yield {
             selected : self.lookup_val == self.user.pk,
             query_string : cl.get_query_string({ %s__not  % self.field.name: self.user.pk}),
             display : capfirst( %s Others  % self.field.verbose_name)
        }

It doesn t seem that Django supports filtering in the negative like this. I ve also experimented with having it do a __gte and __lte but the filterspec only uses the first one it finds (gte), dropping the other (lte).

Anybody know how to achieve a negative filter like this through a custom FilterSpec?

最佳回答

This feature is not part of the Django code yet; it is planned for version 1.2. You ll need to apply this patch to the Django code: http://code.djangoproject.com/ticket/5833

Use the get_query_set() method of FilterSpec. For example:

class AlunoStatus(FilterSpec):
    def __init__(self, request, params, model, model_admin):
        self.lookup_val = request.GET.get( exclude_value , None)

    def get_query_set(self, cl, qs):
        if self.lookup_val:
            qs = qs.exclude(field=self.lookup_val)
        return qs

    def choices(self, cl):
        yield { selected : self.lookup_val is None,
                query_string : cl.get_query_string({}, [ exclude_value ]),
                display : _( All )}
        for choice in choices:
            yield { selected : self.lookup_val == choice,
                     query_string : cl.get_query_string({ exclude_value : choice}),
                     display : u"Exclude "+smart_unicode(choice)}

I didn t test this, but I hope you get the idea.

问题回答

Won t using a "exclude" filter for the negation work?





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签