本文整理汇总了Python中django.contrib.admin.filterspecs.FilterSpec类的典型用法代码示例。如果您正苦于以下问题:Python FilterSpec类的具体用法?Python FilterSpec怎么用?Python FilterSpec使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FilterSpec类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
def render(self, context):
filters = []
request = context['request']
app_name = self.app_name.resolve(context)
model_name = self.model_name.resolve(context)
field_name = self.field_name.resolve(context)
if self.choices_queryset is not None:
choices_queryset = self.choices_queryset.resolve(context)
else:
choices_queryset = None
model = models.get_model(app_name, model_name)
qsm = QueryStringManager(request)
params = qsm.get_params()
if params.get('page', None):
del params['page']
field = model._meta.get_field(field_name)
if field.get_internal_type() == 'FileField':
spec = FieldAvailabilityValueFilterSpec(field, request, params, model, None)
elif self.multiselection and (field.rel is not None): # is a related field (ForeignKeyField)
spec = MultipleRelatedFilterSpec(field, request, params, model, None, choices_queryset)
else: # normal filter spec
spec = FilterSpec.create(field, request, params, model, None)
filter_context = {'title': spec.title(), 'choices' : list(spec.choices(qsm))}
tpl = loader.get_template('admin/filter.html')
return tpl.render(Context(dict(filter_context, autoescape=context.autoescape)))
示例2: get_filters
def get_filters(self, request):
filter_specs = []
if self.list_filter:
filter_fields = [self.lookup_opts.get_field(field_name) for field_name in self.list_filter]
for f in filter_fields:
spec = FilterSpec.create(f, request, self.params, self.model, self.model_admin)
if spec and spec.has_output():
filter_specs.append(spec)
return filter_specs, bool(filter_specs)
示例3: get_filters
def get_filters(self, request):
filter_specs = []
if self.list_filter:
for filter_name in self.list_filter:
field = get_fields_from_path(self.model, filter_name)[-1]
spec = FilterSpec.create(field, request, self.params,
self.model, self.model_admin,
field_path=filter_name)
if spec and spec.has_output():
filter_specs.append(spec)
return filter_specs, bool(filter_specs)
示例4: get_filters
def get_filters(self, request):
filter_specs = []
if self.list_filter:
lookup_opts = self.lookup_opts
for filter_name in self.list_filter:
if '__' in filter_name:
f = None
model = self.model
path = filter_name.split('__')
for field_name in path[:-1]:
f = model._meta.get_field(field_name)
model = f.rel.to
f = model._meta.get_field(path[-1])
spec = FilterSpec.create(f, request, self.params, model, self.model_admin, field_path=filter_name)
else:
f = lookup_opts.get_field(filter_name)
spec = FilterSpec.create(f, request, self.params, self.model, self.model_admin)
if spec and spec.has_output():
filter_specs.append(spec)
return filter_specs, bool(filter_specs)
示例5: get_filters
def get_filters(self, request):
filter_specs = []
if self.list_filter:
filter_fields = []
for field_name in self.list_filter:
if '__' in field_name:
names = field_name.split('__')
field = FakeField(name = names[0], rel_fields = names)
else:
field = self.lookup_opts.get_field(field_name)
filter_fields.append(field)
for f in filter_fields:
spec = FilterSpec.create(f, request, self.params, self.model, self.model_admin)
if spec and spec.has_output():
filter_specs.append(spec)
return filter_specs, bool(filter_specs)
示例6: get_filters
def get_filters(self, request):
# Esto lo modificamos para que en el filtro solo aparezcan opciones con
# datos (por ejemplo, en la vista dispositivos solo aparezcan en el
# filtro de localidad las localidades que tienen dispositivos
filter_specs = []
if self.list_filter:
for filter_name in self.list_filter:
field = get_fields_from_path(self.model, filter_name)[-1]
spec = FilterSpec.create(field, request, self.params,
self.model, self.model_admin,
field_path=filter_name)
#Salva:
if isinstance(spec, RelatedFilterSpec) and not '__' in filter_name:
spec.lookup_choices = [(x._get_pk_val(),x.__unicode__()) for x in field.rel.to.objects.annotate(num_rel=Count(self.model._meta.verbose_name)).filter(num_rel__gt=0)] #IGNORE:W0212
#Fin Modificacion
if spec and spec.has_output():
filter_specs.append(spec)
return filter_specs, bool(filter_specs)
示例7: __init__
def __init__(self, f, request, params, model, model_admin, **kwargs):
filter_by_key = f.name+'_fk_filter_by'
filter_by_val = getattr(model_admin, filter_by_key, None)
filter_related_key = f.name+'_fk_filter_related_only'
filter_related_val = getattr(model_admin, filter_related_key, False)
filter_nf_key = f.name+'_fk_filter_display_field'
if filter_by_val:
filter_nf_val = getattr(model_admin, filter_nf_key, filter_by_val)
else:
filter_nf_val = getattr(model_admin, filter_nf_key, 'pk')
filter_crit_key = f.name+'_fk_filter_criteria_fn'
filter_crit_fn = getattr(model_admin, filter_crit_key, None)
filter_qs_key = f.name+'_fk_filter_query_string'
self.filter_qs_val = getattr(model_admin, filter_qs_key, {})
if filter_by_val:
self.fk_filter_on = True
# we call FilterSpec constructor, not RelatedFilterSpec
# constructor; RelatedFilterSpec constructor will try to
# get all the pk values on the related models, which we
# won't need.
FilterSpec.__init__(self, f, request, params, model, model_admin)
filter_name_key = f.name+'_fk_filter_name'
filter_name_val = getattr(model_admin, filter_name_key, None)
if filter_name_val is None:
self.lookup_title = f.verbose_name
else:
self.lookup_title = f.verbose_name+' '+filter_name_val
self.lookup_kwarg = f.name+'__'+filter_by_val+'__exact'
self.lookup_val = request.GET.get(self.lookup_kwarg, None)
if filter_related_val:
try:
qs = model_admin.queryset(request)
if filter_crit_fn is not None:
qs = filter_crit_fn(qs)
qs = qs.values_list((f.name + '__' + filter_by_val,
f.name + '__' + filter_nf_val)
.order_by(f.name + '__' +
filter_nf_val).distinct())
except Exception:
# Django QuerySet can't follow generic
# relationships using __, so we have to use
# f.rel.to.objects
qs = f.rel.to.objects
if filter_crit_fn is not None:
qs = filter_crit_fn(qs)
qs = qs.values_list(filter_by_val,
filter_nf_val).distinct()
else:
qs = f.rel.to.objects
if filter_crit_fn is not None:
qs = filter_crit_fn(qs)
qs = qs.values_list(filter_by_val, filter_nf_val).distinct()
self.lookup_choices = list(set(qs))
self.lookup_choices.sort(reverse=True)
# if there was a further limiting criteria, then we want
# to make sure we still display the filter even if there
# is only one option
if filter_crit_fn is not None and len(self.lookup_choices) == 1:
self.lookup_choices = self.lookup_choices+[('', ''), ]
else:
self.fk_filter_on = False
RelatedFilterSpec.__init__(self, f, request, params, model,
model_admin)
if filter_related_val:
qs = model_admin.queryset(request)
if filter_crit_fn is not None:
qs = filter_crit_fn(qs)
qs = qs.values_list((f.name+'__pk', f.name+'__'+filter_nf_val)
.order_by(f.name+'__'+filter_nf_val)
.distinct())
self.lookup_choices = list(qs)
示例8: __init__
def __init__(self, f, request, params, model, model_admin):
FilterSpec.__init__(self, f, request, params, model, model_admin)
self.model = model
self.lookup_val = request.GET.get(f, None)
示例9: title
self.lookup_val = request.GET.get(self.lookup_kwarg, None)
self.lookup_choices = f.get_choices(include_blank=False)
def title(self):
return self.lookup_title
def choices(self, cl):
yield {'selected': self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
'display': _('All')}
for pk_val, val in self.lookup_choices:
yield {'selected': self.lookup_val == smart_unicode(pk_val),
'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}, [self.lookup_kwarg]),
'display': val}
FilterSpec.register_front(lambda f: bool(f.rel) and (not hasattr(f, 'null') or not f.null), RelatedFilterSpec)
#==============================================================================
class RelatedNullFilterSpec(FilterSpec):
def __init__(self, f, request, params, model, model_admin,
field_path=None):
super(RelatedNullFilterSpec, self).__init__(f, request, params, model, model_admin)
if isinstance(f, models.ManyToManyField):
self.lookup_title = f.rel.to._meta.verbose_name
else:
self.lookup_title = f.verbose_name
self.null_lookup_kwarg = '%s__isnull' % f.name
self.null_lookup_val = request.GET.get(self.null_lookup_kwarg, None)
rel_name = f.rel.get_related_field().name
self.lookup_kwarg = '%s__%s__exact' % (f.name, rel_name)
示例10: has_output
def has_output(self):
return len(self.lookup_choices) > 1
def title(self):
return self.lookup_title
def choices(self, cl):
yield {'selected': self.lookup_val is None,
'query_string': '?',
'display': _('All')}
for up in self.lookup_choices:
yield {'selected': self.lookup_val == smart_unicode(up.id),
'query_string': cl.get_query_string({self.lookup_kwarg: up.id}),
'display': up.name_cn}
FilterSpec.register_first(lambda f: bool(f.name == 'visit_begin'), AreaFilterSpec)
class DealerTypeFilterSpec(FilterSpec):
def __init__(self, f, request, params, model, model_admin):
super(DealerTypeFilterSpec, self).__init__(f, request, params, model, model_admin)
self.lookup_title = u'经销商'
#rel_name = f.rel.get_related_field().name
#c = f.name
#xx = f.rel.get_related_field()
self.lookup_kwarg = DEALER_TYPE_VAR
self.lookup_val = request.GET.get(DEALER_TYPE_VAR, None)
self.lookup_choices = DealerType.objects.all().order_by('id')
def has_output(self):
return len(self.lookup_choices) > 1
示例11: _register_front
yield {
'selected' : self.file_exists_lookup_val is not None,
'query_string' : cl.get_query_string({self.file_exists_lookup_kwarg : ""}, [self.file_not_exists_lookup_kwarg]),
'display' : 'PreGenCSV',
}
yield {
'selected' : self.file_not_exists_lookup_val is not None,
'query_string' : cl.get_query_string({self.file_not_exists_lookup_kwarg : ""}, [self.file_exists_lookup_kwarg]),
'display' : 'HUB-Core',
}
def _register_front(cls, test, factory):
cls.filter_specs.insert(0, (test, factory))
FilterSpec.register_front = classmethod(_register_front)
FilterSpec.register_front(lambda f: isinstance(f, FileNameField), IndicatorSourceListFilter)
# Actions available in Core
def batch_debug_indicators(modeladmin, request, queryset):
""" Run selected indicators, and store output and a "debug" csv
Each call to this view generates a view batch folder. The contents of the
folder:
- [indicator name].csv for each indicator selected
- [indicator name]_debug.csv for each indicator selected
- batch.log
The indicator generation is scheduled in celery, and output is directed to
batch.log.
"""
示例12: NullFilterSpec
class NullFilterSpec(FilterSpec):
def __init__(self, f, request, params, model, model_admin, field_path=None):
super(NullFilterSpec, self).__init__(f, request, params, model, model_admin)
self.lookup_kwarg = '%s__isnull' % f.name
self.lookup_val = request.GET.get(self.lookup_kwarg, None)
def choices(self, cl):
yield {'selected': self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
'display': _('All')}
for k, v in ((True,_('Null')),('',_('With value'))):
yield {'selected': k == self.lookup_val,
'query_string': cl.get_query_string({self.lookup_kwarg: k}),
'display': v}
FilterSpec.register(lambda f: f.null, NullFilterSpec)
from django.db import models
class DateRangeFilterSpec(FilterSpec):
USE_OUTPUT_FUNC = True
template = 'xadmin/widgets/date_range_filter.html'
def __init__(self, f, request, params, model, model_admin, field_path=None):
super(DateRangeFilterSpec, self).__init__(f, request, params, model, model_admin)
self._from = request.GET.get('born__gte', "")
self._to = request.GET.get('born__lte', "")
def title(self):
return self.field.verbose_name
示例13: __init__
def __init__(self, f, request, params, model, model_admin):
field = get_fields_from_path(model, f)[-1]
FilterSpec.__init__(self, field, request, params, model, model_admin, field_path=f)
self.model = model
self.lookup_val = request.GET.get(f, None)
示例14: PermsFilterSpec
class PermsFilterSpec(FilterSpec):
def __init__(self, f, request, params, model, model_admin):
super(PermsFilterSpec, self).__init__(f, request, params, model, model_admin)
self.lookup_title = f.verbose_name
#rel_name = f.rel.get_related_field().name
#c = f.name
#xx = f.rel.get_related_field()
self.lookup_kwarg = PERMS_VAR
self.lookup_val = request.GET.get(PERMS_VAR, None)
self.lookup_choices = UserPermission.objects.all().order_by('id')
def has_output(self):
return len(self.lookup_choices) > 1
def title(self):
return self.lookup_title
def choices(self, cl):
yield {'selected': self.lookup_val is None,
'query_string': '?',
'display': _('All')}
for up in self.lookup_choices:
yield {'selected': self.lookup_val == smart_unicode(up.id),
'query_string': cl.get_query_string({self.lookup_kwarg: up.id}),
'display': up.name}
FilterSpec.register_first(lambda f: bool(f.name == 'user_permissions'), PermsFilterSpec)
示例15: __init__
accords = models.TextField(u'Аккорды', blank=True)
translation = models.TextField(u'Перевод песни', blank=True)
accords.test_null_filter = True
translation.test_null_filter = True
"""
def __init__(self, f, request, params, model, model_admin):
super(EmptyStringFilterSpec, self).__init__(f, request, params, model, model_admin)
self.lookup_kwarg = '%s__exact' % f.name
self.lookup_kwarg2 = '%s__gt' % f.name
self.lookup_kwarg3 = '%s__isnull' % f.name
self.lookup_val = request.GET.get(self.lookup_kwarg, None)
self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None)
def title(self):
return u"наличие поля '%s'" % self.field.verbose_name
def choices(self, cl):
yield {'selected': self.lookup_val2 is None and self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.lookup_kwarg2, self.lookup_kwarg, self.lookup_kwarg3]),
'display': u'Все'}
yield {'selected': self.lookup_val2 == '' and self.lookup_val is None,
'query_string': cl.get_query_string({self.lookup_kwarg2: ''}, [self.lookup_kwarg, self.lookup_kwarg3]),
'display': u'Имеется'}
yield {'selected': self.lookup_val == '' and self.lookup_val2 is None,
'query_string': cl.get_query_string({self.lookup_kwarg: ''}, [self.lookup_kwarg2, self.lookup_kwarg3]),
'display': u'Отсутствует'}
FilterSpec.register_first(lambda f: hasattr(f, 'test_null_filter'), EmptyStringFilterSpec)