Created
February 26, 2013 20:00
-
-
Save matagus/5041637 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from django.contrib.admin import SimpleListFilter | |
from django.utils.translation import ugettext_lazy as _ | |
from object_images.models import ObjectImage | |
class ObjectImagesListFilter(SimpleListFilter): | |
title = _("object images") | |
parameter_name = "images" | |
def lookups(self, request, model_admin): | |
return ( | |
("with", _("whith images")), | |
("without", _("whitout images")), | |
) | |
def queryset(self, request, queryset): | |
if not self.value(): | |
return queryset | |
app_label = queryset.model._meta.app_label | |
model_name = queryset.model._meta.module_name | |
object_ids = ObjectImage.objects.filter(content_type__app_label=app_label, | |
content_type__model=model_name).distinct().values_list("object_id", flat=True) | |
if self.value() == "with": | |
query_function = queryset.filter | |
elif self.value() == "without": | |
query_function = queryset.exclude | |
return query_function(id__in=object_ids) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment