繁体   English   中英

Django:不允许过滤%filter%

[英]Django: Filtering by %filter% not allowed

我继承了Django v1.2.4应用程序,并且正在添加一些修复和改进。 在此过程中,我突然开始遇到以下错误:

SuspiciousOperation at
/hometeam/admin/players/playeryear/

Filtering by team__season__season_start_date__year not allowed

当我尝试为输入字段选择项目时(通过与字段关联的放大镜访问),此错误将显示在管理界面弹出窗口中。

我已打开调试,但我无法确定此错误发生的位置或最近的更改导致它启动。 你能帮我正确地解析调试输出来追踪导致这个问题的错误过滤器吗?

players / admin.py包含以下类:

class PlayerYearAdmin(FkAutocompleteAdmin):
    related_search_fields = {
        'team': ('school__school',),
        'player': ('first_name', 'last_name'),
    }
    list_display = ['player', 'team', 'player_year_in_school']
    list_filter = ['team']
    search_fields = ['player__first_name', 'player__last_name']
    ordering = ['player__last_name', 'player__first_name']

注释掉list_displaylist_filter语句不会改变问题。

下面是一些调试输出。 我可以根据需要发布更多内容。

Request Method: GET

Request URL:    http://204.232.208.57:8010/hometeam/admin/players/playeryear/?team__season__season_start_date__year=2010&team__sport__sport=Boys%20Basketball&t=id&pop=1

Django Version: 1.2.4

Exception Type: SuspiciousOperation

Exception Value:    Filtering by team__season__season_start_date__year not allowed

Exception Location: /usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/contrib/admin/views/main.py in get_query_set, line 193

Python Executable:  /usr/bin/python

我已经应用了https://code.djangoproject.com/changeset/15140上建议的补丁,但补丁后没有变化。 任何指导将不胜感激。

此问题已根据Chris Adams博客提供的说明解决。 Django 1.2.4引入了一种新的安全功能,限制了使用“查询字符串中的任意跨模型查找”的能力,正如Daniel Roseman在他的回答中所指出的那样。

此版本的解决方法是在FooAdmin中定义一个lookup_allowed方法(在我的例子中为'PlayerYearAdmin' ),对于您希望启用的所有过滤器,它返回true。 就我而言, lookup_allowed看起来像这样:

def lookup_allowed(self, key):
    if key in ('team__season__season_start_date__year', 'team__sport'):
        return True
    return super(PlayerYearAdmin, self).lookup_allowed(key)

您还可以完全绕过安全检查,有效地说明所有查找都是允许的。 这是1.2.4版之前的默认行为:

def lookup_allowed(self, key):
    return True

值得注意的是,版本1.2.5为lookup_allowed 添加了第三个参数 如果您使用的是该版本,则可以像这样定义lookup_allowed

def lookup_allowed(self, key, value):
    if key in ('team__season__season_start_date__year', 'team__sport'):
        return True
    return super(PlayerYearAdmin, self).lookup_allowed(key, value)

作为1.2.4状态的发行说明,不再允许通过查询字符串进行任意跨模型查找,因为它们存在安全风险。 该补丁并不意味着重新启用它们。

您需要在admin的list_filter属性中明确指定允许的关系。 不幸的是,这只能从版本1.3开始,因此您需要升级。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM