def __init__(self, f, request, params, model, *args, **kwargs): super(FkFilterSpec, self).__init__(f, request, params, model, *args, **kwargs) other_model = get_model_from_relation(f) rel_name = other_model._meta.pk.name # ******* Extract parameters ******** the_args = f.fk_filterspec.copy() # The field of the related table fk_field = the_args["fk_field"] other_model2 = get_model_from_relation(other_model._meta.get_field(fk_field)) fk_field_rel_name = other_model2._meta.pk.name # The name in the related table to use as label in the choices label = the_args.pop("label", "") # the foreign key field. By default the field the filter is assigned fk = the_args.pop("fk", f.name) # ******* Build the filter definition ******** self.lookup_kwarg_fk = "{0}__{1}__{2}__exact".format(fk, fk_field, fk_field_rel_name) self.lookup_kwarg = "{0}__{1}__exact".format(fk, rel_name) # self.lookup_kwarg_fk, self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val_fk = request.GET.get(self.lookup_kwarg_fk, None) self.lookup_labels = {} # get the list of values # if label: # label_field = '{0}__{1}'.format(fk, label) # else: # label_field = '{0}'.format(fk) # filter_field = '{0}__{1}'.format(fk, fk_field) # values_list = model.objects.filter().values_list(fk,label_field) # values_list = model.objects.select_related(fk).filter(**{filter_field:self.lookup_val_fk }).values_list(fk,label_field) values_list = ( other_model.objects.select_related() .filter(**{fk_field: self.lookup_val_fk}) .values_list(fk_field_rel_name, label) ) # if self.lookup_val: for (v, l) in values_list: self.lookup_labels[v] = l if self.lookup_val: values_list = other_model.objects.filter(**{rel_name: self.lookup_val}).values_list( fk_field_rel_name, label ) for (v, l) in values_list: self.lookup_labels[v] = l self.lookup_choices = self.lookup_labels.keys()
def __init__(self, f, request, params, model, model_admin, field_path=None): super(MpttParentFieldFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path) from shop.models import Category queryset = Category.objects.all() self.lookup_choices = [(x._get_pk_val(), x) for x in queryset] other_model = get_model_from_relation(f) if isinstance(f, (models.ManyToManyField, models.related.RelatedObject)): self.lookup_title = other_model._meta.verbose_name else: self.lookup_title = f.verbose_name rel_name = other_model._meta.pk.name self.lookup_kwarg = '%s__%s__in' % (self.field_path, rel_name) self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull, None)
def __init__(self, field, request, params, model, model_admin, field_path): self.field = field self.field_path = field_path self.used_parameters = {} self.lookup_kwarg_isnull = '%s__isnull' % field_path other_model = get_model_from_relation(field) rel_name = other_model._meta.pk.name if "tags__tag_category__name__iexact" in request.GET: self.lookup_kwarg = '%s__description' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) # getting the first char of values cat_t = request.GET.get("tags__tag_category__name__iexact") cat = TagCategory.objects.get(name__iexact=cat_t) tags = Tag.objects.filter(tag_category__id__exact="%s" % cat.pk) self.lookup_choices = list(tag.description for tag in tags) self.lookup_choices.sort() else: self.lookup_kwarg = '%s__tag_category__name__iexact' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) # getting the first char of values self.lookup_choices = list( cat.name for cat in TagCategory.objects.all()) self.lookup_choices.sort() for p in self.expected_parameters(): if p in params: value = params.pop(p) self.used_parameters[p] = prepare_lookup_value(p, value) # super(RelatedFieldListFilter, self).__init__( # field, request, params, model, model_admin, field_path) self.title = _('tag category')
def __init__(self, field, request, params, model, model_admin, field_path): """cut/pasted from RelatedFieldListFilter parent constructor, with db hit removed""" other_model = get_model_from_relation(field) if hasattr(field, 'rel'): rel_name = field.rel.get_related_field().name else: rel_name = other_model._meta.pk.name self.lookup_kwarg = '%s__%s__exact' % (field_path, rel_name) self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull, None) # this is the one change from the parent constructor. # instead of getting all choices from the table, only pick one if theres already one set so we can display it self.lookup_choices = [(-1,""),(-2,"")] # needs at least TWO or admin wont show it if empty. if self.lookup_val: try: obj = field.rel.to.objects.get(pk=self.lookup_val) val = obj.__unicode__() except field.rel.to.DoesNotExist: val = "" pass self.lookup_choices.append((self.lookup_val,val)) # note we are deliberately calling our parent's parent constructor super(RelatedFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path) if hasattr(field, 'verbose_name'): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title
def __init__(self, f, request, params, model, model_admin, field_path=None): super(RelatedFilterSpec, 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__exact' % (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) # Get tags and their count through_opts = f.through._meta count_field = ("%s_%s_items" % (through_opts.app_label, through_opts.object_name)).lower() queryset = getattr(f.model, f.name).all() queryset = queryset.annotate(num_times=Count(count_field)) queryset = queryset.order_by("-num_times") self.lookup_choices = [(t.pk, "%s (%s)" % (t.name, t.num_times)) for t in queryset]
def __init__(self, f, request, params, model, model_admin, field_path=None): super(RelatedFilterSpec, 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__exact' % (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) self.lookup_choices = f.get_choices(include_blank=False)
def __init__(self, f, request, params, model, model_admin, field_path=None): #BAD HACK!!! super(RelatedFilterSpec, self).__init__( f, request, params, model, model_admin, field_path=field_path) # others stored in parent self.request = request self.model = model self.model_admin = model_admin 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__exact' % (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) self.lookup_choices = self.get_lookup_choices()
def __init__(self, field, request, params, model, model_admin, field_path): self.field = field self.field_path = field_path self.used_parameters = {} self.lookup_kwarg_isnull = '%s__isnull' % field_path other_model = get_model_from_relation(field) rel_name = other_model._meta.pk.name if "tags__tag_category__name__iexact" in request.GET: self.lookup_kwarg = '%s__description' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) # getting the first char of values cat_t = request.GET.get("tags__tag_category__name__iexact") cat = TagCategory.objects.get(name__iexact=cat_t) tags = Tag.objects.filter(tag_category__id__exact="%s" % cat.pk) self.lookup_choices = list(tag.description for tag in tags) self.lookup_choices.sort() else: self.lookup_kwarg = '%s__tag_category__name__iexact' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) # getting the first char of values self.lookup_choices = list(cat.name for cat in TagCategory.objects.all()) self.lookup_choices.sort() for p in self.expected_parameters(): if p in params: value = params.pop(p) self.used_parameters[p] = prepare_lookup_value(p, value) # super(RelatedFieldListFilter, self).__init__( # field, request, params, model, model_admin, field_path) self.title = _('tag category')
def get_context_data(self, *args, **kwargs): context = super(FilteredViewMixin, self).get_context_data(*args, **kwargs) # Add in the queryset used for this filtered view context.update({"view_filter_kwargs": self.get_filter_kwargs()}) # Add in the object that's been filtered on if there's only one or if it's been explicitly provided if self.context_slug or len(self.get_merged_kwargs()) == 1: context_pointer = self.model context_slug = self.context_slug or self.get_merged_kwargs().keys( )[0] field_names = context_slug.split(LOOKUP_SEP) try: for field_name in field_names: field_name = self.trim_request_user(field_name) if field_name == "pk": field_name = context_pointer._meta.pk.name context_pointer = get_model_from_relation( context_pointer._meta.get_field_by_name(field_name)[0]) field_name = "pk" field_value = context["view_filter_kwargs"][ self.trim_request_user(context_slug)].pk except NotRelationField: field_name = field_names[-1] field_value = context["view_filter_kwargs"][context_slug] context["context_object"] = context_pointer.objects.get( **{field_name: field_value}) return context
def __init__(self, field, field_path, index): other_model = get_model_from_relation(field) rel_name = other_model._meta.pk.name self.lookup_kwarg = '%s__%s__exact' % (field_path, rel_name) self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_choices = field.get_choices(include_blank=False) super(RelatedFieldFilter, self).__init__( field, field_path, index) if hasattr(field, 'verbose_name'): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) if hasattr(field, 'rel'): rel_name = field.rel.get_related_field().name else: rel_name = other_model._meta.pk.name self.lookup_kwarg1 = '%s__%s' % (field_path, rel_name) self.lookup_val1 = request.GET.get(self.lookup_kwarg1, None) self.lookup_kwarg2 = '%s__%s__in' % (field_path, rel_name) self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None) super(RelatedFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path) to_int = lambda v: None if v == "" else int(v) for kwarg in (self.lookup_kwarg, self.lookup_kwarg1): if kwarg in self.used_parameters: val = to_int(self.used_parameters[kwarg]) if val is None: del self.used_parameters[kwarg] else: self.used_parameters[kwarg] = val if self.lookup_kwarg2 in self.used_parameters: if isinstance(self.used_parameters[self.lookup_kwarg2], (list, tuple)): vals = None for v in self.used_parameters[self.lookup_kwarg2]: val = to_int(v) if val is None: continue if vals is None: vals = [val] else: vals.append(val) if vals is None: del self.used_parameters[self.lookup_kwarg2] elif len(vals) == 1: del self.used_parameters[self.lookup_kwarg2] self.used_parameters[self.lookup_kwarg] = vals[0] else: self.used_parameters[self.lookup_kwarg2] = vals else: val = to_int(self.used_parameters[self.lookup_kwarg2]) if val is None: del self.used_parameters[self.lookup_kwarg2] else: del self.used_parameters[self.lookup_kwarg2] self.used_parameters[self.lookup_kwarg] = val
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) rel_name = other_model._meta.pk.name self.lookup_kwarg = "%s__%s__exact" % (field_path, rel_name) self.lookup_kwarg_isnull = "%s__isnull" % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull, None) self.lookup_choices = field.get_choices(include_blank=False) super(RelatedFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path) if hasattr(field, "verbose_name"): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title
def __init__(self, f, request, params, model, model_admin, field_path=None): super(RelatedFilterSpec, 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__exact' % (self.field_path, rel_name) self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_choices = f.get_choices(include_blank=False)
def __init__(self, field, request, params, model, model_admin, field_path): other_model = get_model_from_relation(field) rel_name = other_model._meta.pk.name self.lookup_kwarg = '%s__%s__exact' % (field_path, rel_name) self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val_isnull = request.GET.get( self.lookup_kwarg_isnull, None) self.lookup_choices = field.get_choices(include_blank=False) super(RelatedFieldListFilter, self).__init__( field, request, params, model, model_admin, field_path) if hasattr(field, 'verbose_name'): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title
def __init__(self, field, request, params, model, model_admin, field_path): """cut/pasted from RelatedFieldListFilter parent constructor, with db hit removed""" other_model = get_model_from_relation(field) if hasattr(field, 'rel'): rel_name = field.rel.get_related_field().name else: rel_name = other_model._meta.pk.name self.lookup_kwarg = '%s__%s__exact' % (field_path, rel_name) self.lookup_kwarg_isnull = '%s__isnull' % field_path self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull, None) # this is the one change from the parent constructor. # instead of getting all choices from the table, only pick one if theres already one set so we can display it self.lookup_choices = [ (-1, ""), (-2, "") ] # needs at least TWO or admin wont show it if empty. if self.lookup_val: try: obj = field.rel.to.objects.get(pk=self.lookup_val) val = obj.__unicode__() except field.rel.to.DoesNotExist: val = "" pass self.lookup_choices.append((self.lookup_val, val)) # note we are deliberately calling our parent's parent constructor super(RelatedFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path) if hasattr(field, 'verbose_name'): self.lookup_title = field.verbose_name else: self.lookup_title = other_model._meta.verbose_name self.title = self.lookup_title