【问题标题】:django admin filters and mongodb: Caught DatabaseError while rendering: This query is not supported by the databasedjango admin过滤器和mongodb:渲染时捕获DatabaseError:数据库不支持此查询
【发布时间】:2012-10-19 18:46:09
【问题描述】:

使用 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',所以原因是这样的)。

【问题讨论】:

  • 不知道为什么会被否决。我遇到了完全相同的问题,并且认为不应该有通过 CharField 过滤所需的连接...
  • 我已经编辑了问题,并将其添加到有问题的位置
  • 请显示您的模型代码和该模型的完整管理员定义。
  • 编辑了问题,现在它包括模型和 ModelAdmin 定义

标签: django mongodb django-models filter django-admin


【解决方案1】:

所以基本上你只想从 mongo 中检索一些字段,不是吗?

看着tutorial,相信你在Django View 或Template 中一定会看到这个

您也可以尝试Google group 了解更多信息

【讨论】:

  • 不,我想创建 django 管理过滤器docs.djangoproject.com/en/dev/ref/contrib/admin/…
  • 您需要使用 nosql django fork 才能与 mongodb 兼容。 (Django MongoDB Engine 使用 Django 1.3 的分叉版本,增加了非关系支持。)
  • 我已经卸载了 django 并安装了 django-nonrel,但没有任何变化:(
【解决方案2】:

myfield 是什么类型的?您不能按关系排序。

【讨论】:

    【解决方案3】:

    Mongo-Db-Engine 不支持管理员过滤器所需的查询,如 here 所述

    【讨论】:

      【解决方案4】:

      发生错误是因为 Django Admin 的 list_filters 在尝试确定哪些成员属于列表时使用了distinct()

      我们的(内部,hacky)解决方案是修补 django non_rel 以使不同的调用发生在内存中,而不是在 DB 端。这绝不是“正确”的解决方案,我们也没有在其他用例中测试它,但总比没有好。

      YMMV.

      https://github.com/BlueDragonX/django-nonrel/commit/4025327efbe5c17c6b77e0497c2b433819c42918

      【讨论】:

      • 为什么它在内存中而不是在数据库中进行不同的调用? PS:感谢提交
      【解决方案5】:

      我是 Python/Django 新手,但我设法通过从 https://github.com/django/django/blob/stable/1.4.x/django/contrib/admin/filters.py 复制 AllValuesFieldListFilter 来解决此问题(确保将您的分支更改为您的 Django 版本),然后我删除了对 distinct 的调用。不得不进口一堆东西。然后它工作。我还应用了@AlexeyMK 答案中的更改以使其再次与众不同。

      对于 1.4:

      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,
              }
      

      然后像这样指定它使用这个过滤器:

      list_filter = (('myfield', MongoFieldListFilter),)
      

      很好,因为不需要打补丁。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-11
        • 1970-01-01
        • 2016-09-05
        • 2014-01-30
        相关资源
        最近更新 更多