将django non-rel 1.3与MongoDB一起用作数据库后端:
我正在尝试在相应的django ModelAdmin类的MyModel类的字段(CharField)上设置过滤器,如下所示:
class MyModelAdmin(admin.ModelAdmin):
list_filter = ('myfield',)
但我得到的是:
TemplateSyntaxError at /admin/myapp/mymodel
Caught DatabaseError while rendering: This query is not supported by the database.
似乎Django MongoDB引擎不支持过滤器,但我找不到文档。
编辑: 错误来自模板文件... / admin / templates / change_list.html,抛出它的行是第85行:
{% for spec in cl.filter_specs %}{% admin_list_filter cl spec %}{% endfor %}
我的模特是:
class MyModel(models.Model):
myfield=models.CharField(max_length='300')
我的管理模式是:
class MyModelAdmin(admin.ModelAdmin):
list_filter = ('myfield',)
并将其注册为:
admin.site.register(MyModel, MyModelAdmin)
。
调试代码时,异常由basecompiler.py的方法check_query
抛出。此方法验证self.query.distinct or self.query.extra or self.query.having
是否为真,然后抛出异常(self.query.distinct在感兴趣的查询对象中等于'True',因此原因是这样。)
答案 0 :(得分:1)
我是一个Python / Django newb,但我设法通过从https://github.com/django/django/blob/stable/1.4.x/django/contrib/admin/filters.py复制AllValuesFieldListFilter来解决这个问题(确保将你的分支更改为你的Django版本)然后我删除了对{{1 }}。不得不导入一堆东西。然后它工作。我还应用了来自@ AlexeyMK的答案的变化,使其再次变得清晰。
for 1.4:
distinct
然后将其指定为使用此过滤器,如下所示:
from django.contrib.admin.filters import FieldListFilter
from django.contrib.admin.util import (get_model_from_relation, reverse_field_path, get_limit_choices_to_from_path, prepare_lookup_value)
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_unicode, force_unicode
class MongoFieldListFilter(FieldListFilter):
def __init__(self, field, request, params, model, model_admin, field_path):
self.lookup_kwarg = field_path
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)
parent_model, reverse_path = reverse_field_path(model, field_path)
queryset = parent_model._default_manager.all()
# optional feature: limit choices base on existing relationships
# queryset = queryset.complex_filter(
# {'%s__isnull' % reverse_path: False})
limit_choices_to = get_limit_choices_to_from_path(model, field_path)
queryset = queryset.filter(limit_choices_to)
def uniquify(coll): # enforce uniqueness, preserve order
seen = set()
return [x for x in coll if x not in seen and not seen.add(x)]
self.lookup_choices = uniquify(queryset.order_by(field.name).values_list(field.name, flat=True))
super(MongoFieldListFilter, self).__init__(field, request, params, model, model_admin, field_path)
def expected_parameters(self):
return [self.lookup_kwarg, self.lookup_kwarg_isnull]
def choices(self, cl):
from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
yield {
'selected': (self.lookup_val is None
and self.lookup_val_isnull is None),
'query_string': cl.get_query_string({},
[self.lookup_kwarg, self.lookup_kwarg_isnull]),
'display': _('All'),
}
include_none = False
for val in self.lookup_choices:
if val is None:
include_none = True
continue
val = smart_unicode(val)
yield {
'selected': self.lookup_val == val,
'query_string': cl.get_query_string({
self.lookup_kwarg: val,
}, [self.lookup_kwarg_isnull]),
'display': val,
}
if include_none:
yield {
'selected': bool(self.lookup_val_isnull),
'query_string': cl.get_query_string({
self.lookup_kwarg_isnull: 'True',
}, [self.lookup_kwarg]),
'display': EMPTY_CHANGELIST_VALUE,
}
这很好,因为不需要修补。
答案 1 :(得分:0)
Mongo-Db-Engine不支持管理员过滤器所需的查询,如here所述
答案 2 :(得分:0)
发生错误是因为Django Admin的list_filters在尝试确定哪些成员属于列表时使用distinct()
。
我们的(内部的,hacky)解决方案是修补django non_rel以使不同的调用发生在内存中,而不是发生在DB端。这绝不是“正确的”解决方案,我们还没有在我们之外的用例中测试它,但它总比没有好。
因人而异。
https://github.com/BlueDragonX/django-nonrel/commit/4025327efbe5c17c6b77e0497c2b433819c42918
答案 3 :(得分:-1)
答案 4 :(得分:-1)
myfield
的类型是什么?你不能按关系订购。