我正在尝试将自定义参数传递给 Django Admin 更改列表视图,以便我可以以专门的方式过滤列表。我想根据名为“active_pp”的 GET 参数过滤 2 个字段 start_date 和 end_date 的查询集。我已经让过滤器正常工作,但我无法传入一个 GET 查询参数,该参数指定我应该显示过滤结果还是正常结果。
我知道,出于安全考虑,Django Admin 会过滤掉任何传递给它的与指定模型字段无关的查询参数;发现错误参数后,管理员将用户重定向到当前视图,但将 GET 查询参数替换为 e=1。我想将我的自定义“active_pp”参数列入白名单,这样页面就不会被重定向,我将能够使用该参数。
这是 admin.py 中带有查询集自定义的 ModelAdmin 的示例。
class FeaduredAdmin(admin.ModelAdmin):
....
def get_changelist(self, request, **kwargs):
from django.contrib.admin.views.main import ChangeList
# Try to get the 'active_pp' query parameter
active_pp = request.GET.get('active_pp',None)
# Define a custom ChangeList class with a custom queryset
class ActiveChangeList(ChangeList):
def get_query_set(self, *args, **kwargs):
now = datetime.datetime.now()
qs = super(ActiveChangeList, self).get_query_set(*args, **kwargs)
return qs.filter((Q(start_date=None) | Q(start_date__lte=now))
& (Q(end_date=None) | Q(end_date__gte=now)))
# use the custom ChangeList class if the parameter exists
if active_pp:
return ActiveChangeList
return ChangeList
有谁知道如何将传递给 change_list 的自定义 GET 查询字符串参数列入白名单?
感谢您的阅读和考虑,乔
更新:
使用 Uvasal 提供的链接,我能够正确地将 GET 参数列入白名单。
class ActiveFilterAminForm(forms.Form):
active_pp = forms.CharField()
class FeaduredAdmin(admin.ModelAdmin):
....
# Based on: http://djangosnippets.org/snippets/2322/
advanced_search_form = ActiveFilterAminForm()
def get_changelist(self, request, **kwargs):
from django.contrib.admin.views.main import ChangeList
active_pp = self.other_search_fields.get('active_pp',None)
# now we have the active_pp parameter that was passed in and can use it.
class ActiveChangeList(ChangeList):
def get_query_set(self, *args, **kwargs):
now = datetime.datetime.now()
qs = super(ActiveChangeList, self).get_query_set(*args, **kwargs)
return qs.filter((Q(start_date=None) | Q(start_date__lte=now))
& (Q(end_date=None) | Q(end_date__gte=now)))
if not active_pp is None:
return ActiveChangeList
return ChangeList
def lookup_allowed(self, lookup):
if lookup in self.advanced_search_form.fields.keys():
return True
return super(MyModelAdmin, self).lookup_allowed(lookup)
def changelist_view(self, request, extra_context=None, **kwargs):
self.other_search_fields = {}
asf = self.advanced_search_form
extra_context = {'asf':asf}
request.GET._mutable=True
for key in asf.fields.keys():
try:
temp = request.GET.pop(key)
except KeyError:
pass
else:
if temp!=['']:
self.other_search_fields[key] = temp
request.GET_mutable=False
return super(FeaduredProductAdmin, self)\
.changelist_view(request, extra_context=extra_context)