Skip to content

Instantly share code, notes, and snippets.

@matagus
Created February 26, 2013 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matagus/5041637 to your computer and use it in GitHub Desktop.
Save matagus/5041637 to your computer and use it in GitHub Desktop.
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