Wagtail ModelAdmin with list_filter error: FieldDoesNotExist: Region has no field named 'p'
Wagtail ModelAdmin with list_filter error: FieldDoesNotExist: Region has no field named 'p'
我有一个通过 Wagtail 的 modeladmin
公开的模型,在我尝试应用包含外键的 list_filter
之前一直有效。我有一个名为 Region 的自引用模型(用于存储县、城市和城镇),我希望能够按 Parent
级别创建过滤器。
我是 运行 Django 1.10 上的 Wagtail 1.8.1。这是模型:
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,
)
以及来自 wagtail_hooks.py
的 ModelAdmin
:
class RegionAdmin(ModelAdmin):
model = Region
menu_icon = 'doc-full-inverse'
empty_value_display = 'ROOT'
list_display = ('parent', 'name')
list_filter = ('parent')
modeladmin_register(RegionAdmin)
如果我注释掉 class RegionAdmin
中的 list_filter
行,效果很好。文档说它可以在列表中使用 ForeignKey
。我错过了一些明显的东西吗?堆栈跟踪在这里:
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'
感谢您的帮助或想法!
您缺少一个逗号:
list_filter = ('parent',)
没有逗号,括号将被忽略,django 将迭代字符串,而不是元组(因为没有元组!),因此尝试访问字段 p
.
来自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.
我有一个通过 Wagtail 的 modeladmin
公开的模型,在我尝试应用包含外键的 list_filter
之前一直有效。我有一个名为 Region 的自引用模型(用于存储县、城市和城镇),我希望能够按 Parent
级别创建过滤器。
我是 运行 Django 1.10 上的 Wagtail 1.8.1。这是模型:
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,
)
以及来自 wagtail_hooks.py
的 ModelAdmin
:
class RegionAdmin(ModelAdmin):
model = Region
menu_icon = 'doc-full-inverse'
empty_value_display = 'ROOT'
list_display = ('parent', 'name')
list_filter = ('parent')
modeladmin_register(RegionAdmin)
如果我注释掉 class RegionAdmin
中的 list_filter
行,效果很好。文档说它可以在列表中使用 ForeignKey
。我错过了一些明显的东西吗?堆栈跟踪在这里:
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'
感谢您的帮助或想法!
您缺少一个逗号:
list_filter = ('parent',)
没有逗号,括号将被忽略,django 将迭代字符串,而不是元组(因为没有元组!),因此尝试访问字段 p
.
来自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.