问题描述
使用django non-rel 1.3与MongoDB作为DB后端:我试图在MyModel类的字段(CharField)上设置一个过滤器,相应的django ModelAdmin类,如下所示:
class MyModelAdmin(admin.ModelAdmin):
list_filter =('myfield',)
但我得到的是:
TemplateSyntaxError at / admin / myapp / mymodel
在渲染时捕获DatabaseError:数据库不支持此查询。 / code>
似乎Django MongoDB引擎不支持过滤器,但我没有找到有关的文档。
编辑:
错误来自模板文件... / admin / templates / change_list.html,其中引用的行是第85行:
{%for cl.filter_specs%} {%admin_list_filter cl spec%} {%endfor%}
我的模型是:
class MyModel(models.Mode l):
myfield = models.CharField(max_length ='300')
我的管理模型是
class MyModelAdmin(admin.ModelAdmin):
list_filter =('myfield',)
/ p>
并注册:
admin.site.register MyModel,MyModelAdmin)
。
调试代码,方法抛出异常 check_query basecompiler.py。此方法验证 self.query.distinct或self.query.extra或self.query.having
为true,然后抛出异常(self.query.distinct等于
解决方案
出现错误是因为Django Admin的list_filters使用了
我们的(内部,黑客)解决方案是要修补django non_rel以使内存中发生明显的调用,而不是在DB端。这绝对不是正确的解决方案,而且我们还没有在其他的用例中对它进行测试,但是比没有更好。
YMMV。
Using django non-rel 1.3 with MongoDB as the DB backend:
I'm trying to setting a filter on a field (CharField) of MyModel class, in the corresponding django ModelAdmin class, like this:
class MyModelAdmin(admin.ModelAdmin): list_filter = ('myfield',)
but what i get is:
TemplateSyntaxError at /admin/myapp/mymodel
Caught DatabaseError while rendering: This query is not supported by the database.
It seems that Django MongoDB engine doesn't support filter, but i don't find documentation about.
EDIT:The error comes from template file .../admin/templates/change_list.html, and the line that throws it is line 85:
{% for spec in cl.filter_specs %}{% admin_list_filter cl spec %}{% endfor %}
My model is:
class MyModel(models.Model): myfield=models.CharField(max_length='300')
and my admin model is:
class MyModelAdmin(admin.ModelAdmin): list_filter = ('myfield',)
and register it with:
admin.site.register(MyModel, MyModelAdmin)
.
Debugging the code, the exception is throwed by method check_query
of basecompiler.py. This method verifies that self.query.distinct or self.query.extra or self.query.having
is true and then throws the exception (self.query.distinct is equal to 'True' in the interested query object, so the cause is this).
解决方案 The error occurs because Django Admin's list_filters use a distinct()
when trying to figure out which members belong in the list.
Our (internal, hacky) solution was to patch django non_rel to make the distinct call happen in memory rather then on the DB end. This is by no means the 'correct' solution, and we haven't made a point of testing it in use cases other then ours, but it's better than nothing.
YMMV.
https://github.com/BlueDragonX/django-nonrel/commit/4025327efbe5c17c6b77e0497c2b433819c42918
这篇关于django admin筛选器和mongodb:在渲染时捕获DatabaseError:数据库不支持此查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!