Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #filters.py
- from django.db import models
- from django.utils.encoding import smart_unicode, iri_to_uri
- from django.contrib.admin.util import get_model_from_relation, \
- reverse_field_path, get_limit_choices_to_from_path
- from django.contrib.admin.filterspecs import RelatedFilterSpec
- from models import Category
- class CategoryFilterSpec(RelatedFilterSpec):
- def __init__(self, f, request, params, model, model_admin,
- field_path=None):
- super(CategoryFilterSpec, self).__init__(
- f, request, params, model, model_admin, field_path=field_path)
- other_model = get_model_from_relation(f)
- if isinstance(f, (models.ManyToManyField,
- models.related.RelatedObject)):
- # no direct field on this model, get name from other model
- self.lookup_title = other_model._meta.verbose_name
- else:
- self.lookup_title = f.verbose_name # use field name
- rel_name = other_model._meta.pk.name
- self.lookup_kwarg = '%s__%s__in' % (self.field_path, rel_name)
- self.lookup_kwarg_isnull = '%s__isnull' % (self.field_path)
- self.lookup_val = request.GET.get(self.lookup_kwarg, None)
- self.lookup_val_isnull = request.GET.get(
- self.lookup_kwarg_isnull, None)
- lookup_choices = f.get_choices(include_blank=False)
- self.lookup_choices = []
- for pk_val, val in lookup_choices:
- c = Category.objects.get(pk = pk_val)
- if c.parent == None:
- q = str(pk_val)+','
- for p in Category.objects.filter(parent=pk_val):
- q = q+str(p.pk)+','
- q = q[:-1]
- self.lookup_choices.append((q, val))
- else:
- self.lookup_choices.append((pk_val, val))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement