简体   繁体   English

带有list_filter错误的Wagtail ModelAdmin:FieldDoesNotExist:区域没有名为“ p”的字段

[英]Wagtail ModelAdmin with list_filter error: FieldDoesNotExist: Region has no field named 'p'

I've got a model I'm exposing through Wagtail's modeladmin that works until I try to apply a list_filter including a ForeignKey. 我有一个通过modeladminmodeladmin公开的模型,该模型在尝试应用包括ForeignKey的modeladmin之前一直list_filter I've got a self-referencing model (to store Counties, Cities, and Towns) called Region that I'd like to be able to create a filter on by the Parent level. 我有一个名为Region的自引用模型(用于存储县,市和镇),我希望能够在Parent级上创建过滤器。

I'm running Wagtail 1.8.1 on Django 1.10. 我在Django 1.10上运行Wagtail 1.8.1。 Here's the model: 这是模型:

class Region(models.Model):
    """
    Tree of regions and sub-regions.
    """

    name = models.CharField(max_length=255)
    parent = models.ForeignKey(
        'Region',
        blank=True,
        null=True,
    )

    class Meta:
        unique_together = ('name', 'parent')

    def __str__(self):
        return '{0}'.format(
            self.name,
        )

And the ModelAdmin from wagtail_hooks.py : ModelAdminwagtail_hooks.py

class RegionAdmin(ModelAdmin):
    model = Region
    menu_icon = 'doc-full-inverse'
    empty_value_display = 'ROOT'
    list_display = ('parent', 'name')
    list_filter = ('parent')


modeladmin_register(RegionAdmin)

It works fine if I comment out the list_filter line in the class RegionAdmin . 如果我在类RegionAdmin list_filter行,它会RegionAdmin The documentation says it can take a ForeignKey in the list. 该文档说它可以在列表中包含一个ForeignKey Am I missing something obvious? 我是否缺少明显的东西? Stack trace is here: 堆栈跟踪在这里:

Traceback (most recent call last):
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/django/views/decorators/cache.py", line 43, in _cache_controlled
    response = viewfunc(request, *args, **kw)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/wagtail/wagtailadmin/decorators.py", line 24, in decorated_view
    return view_func(request, *args, **kwargs)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/wagtail/contrib/modeladmin/options.py", line 350, in index_view
    return view_class.as_view(**kwargs)(request)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/wagtail/contrib/modeladmin/views.py", line 249, in dispatch
    self.queryset = self.get_queryset(request)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/wagtail/contrib/modeladmin/views.py", line 554, in get_queryset
    filters_use_distinct) = self.get_filters(request)
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/wagtail/contrib/modeladmin/views.py", line 381, in get_filters
    field_path)[-1]
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/django/contrib/admin/utils.py", line 485, in get_fields_from_path
    fields.append(parent._meta.get_field(piece))
  File "/home/vagrant/.virtualenvs/sepia/lib/python3.5/site-packages/django/db/models/options.py", line 619, in get_field
    raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Region has no field named 'p'

Thanks for any help or ideas! 感谢您的帮助或想法!

You are missing a comma: 您缺少逗号:

list_filter = ('parent',)

Without the comma, the parentheses are ignored and django will be iterating the string, and not a tuple (because there is no tuple!), hence the attempt to access field p . 没有逗号,括号将被忽略,并且django将迭代字符串,而不是元组(因为没有元组!),因此尝试访问字段p

From the docs : 文档

a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). 一个带有一个项目的元组是通过在值后面加上逗号来构造的(仅将一个值括在括号中是不够的)。 Ugly, but effective. 难看,但是有效。

暂无
暂无

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

相关问题 FieldDoesNotExist:ManyToManyField 没有名为 None 的字段 - FieldDoesNotExist: ManyToManyField has no field named None FieldDoesNotExist at /accounts/signup/,用户没有名为“用户名”的字段 - FieldDoesNotExist at /accounts/signup/, User has no field named 'username' django.core.exceptions.FieldDoesNotExist:BuildingAddress没有名为“ False”的字段 - django.core.exceptions.FieldDoesNotExist: BuildingAddress has no field named 'False' Django createsuperuser抛出django.core.exceptions.FieldDoesNotExist:用户没有名为''的字段错误 - Django createsuperuser throws django.core.exceptions.FieldDoesNotExist: User has no field named ' ' error django.core.exceptions.FieldDoesNotExist:用户没有名为“用户名”的字段 - django.core.exceptions.FieldDoesNotExist: User has no field named 'username' 添加 list_filter 时,如何在 django modeladmin 更改列表中修复/设置列宽? - How to fix/set column width in a django modeladmin change list table when a list_filter is added? 在 header 上移动 Wagtail Admin list_filter - 列表过滤器覆盖表数据 - Move Wagtail Admin list_filter on header - List Filter covering table data Django 错误:'list_filter[2]' 的值指的是 'is_active',而不是指一个字段。? - Django error: The value of 'list_filter[2]' refers to 'is_active', which does not refer to a Field.? Django:list_display 接受字段但不接受 list_filter - Django: list_display accepts field but not list_filter django管理页面中用于外键字段的list_filter - list_filter in django admin page for foreign key field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM