Created
March 24, 2014 14:52
-
-
Save elinaldosoft/9741654 to your computer and use it in GitHub Desktop.
Exemplo Filtros Dinâmicos
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
class CategoriaFather(admin.SimpleListFilter): | |
title = 'Categorias' | |
parameter_name = 'parent__id__exact' | |
def lookups(self, request, model_admin): | |
ids = list() | |
names = list() | |
for cat in Categoria().get_roots(): | |
ids.append(cat.id) | |
count = str(Categoria.objects.filter(parent=cat.id).count()) | |
names.append(cat.slug+" ("+count+")") | |
return zip(ids, names) | |
def queryset(self, request, queryset): | |
try: | |
if self.value(): | |
return queryset.filter(parent__id__exact=self.value()) | |
return queryset.filter() | |
except: | |
pass | |
class CategoriaAdmin(admin.ModelAdmin): | |
list_filter = [CategoriaFather] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment