的Django == 1.10.2
尝试在管理视图中使用Group.members
相关属性和模型:
from django.utils.translation import ugettext as _
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
group = models.ForeignKey(
'Group',
verbose_name=_('Groupe'),
related_name='members',
blank=True,
null=True,
)
class Group(models.Model):
name = models.CharField(
max_length=255,
unique=True,
verbose_name=_('Nom'),
error_messages={
'unique': _('Ce nom est déjà utilisée.'),
},
)
creator = models.ForeignKey(
User,
related_name='created_groups',
blank=False,
null=False,
)
def __str__(self):
return self.name
管理员观点:
from django.contrib import admin
from meal.models import Group, User
class GroupAdmin(admin.ModelAdmin):
fields = ('name', 'members')
list_display = ('name', 'members')
search_fields = (
'name',
)
class UserAdmin(admin.ModelAdmin):
fields = ('email', 'last_name', 'first_name', 'group')
list_display = ('email', 'last_name', 'first_name', 'group')
search_fields = (
'username',
'email',
'first_name',
'last_name',
)
admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)
但是管理员查看引发错误:
/ admin / meal / group /'经理'
的KeyError
完成追踪
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/meal/group/
Django Version: 1.10.2
Python Version: 3.5.2
Installed Applications:
['django.contrib.sites',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'meal',
'debug_toolbar',
'bootstrap3',
'allauth',
'allauth.account',
'allauth.socialaccount',
'django_extensions']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Template error:
In template /home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/contrib/admin/templates/admin/change_list.html, error at line 82
manager 72 : {% endif %}
73 : {% endblock %}
74 :
75 : <form id="changelist-form" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %} novalidate>{% csrf_token %}
76 : {% if cl.formset %}
77 : <div>{{ cl.formset.management_form }}</div>
78 : {% endif %}
79 :
80 : {% block result_list %}
81 : {% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %}
82 : {% result_list cl %}
83 : {% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %}
84 : {% endblock %}
85 : {% block pagination %}{% pagination cl %}{% endblock %}
86 : </form>
87 : </div>
88 : </div>
89 : {% endblock %}
90 :
Traceback:
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/contrib/admin/utils.py" in lookup_field
274. f = _get_non_gfk_field(opts, name)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/contrib/admin/utils.py" in _get_non_gfk_field
312. raise FieldDoesNotExist()
During handling of the above exception (), another exception occurred:
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
217. response = self.process_exception_by_middleware(e, request)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
215. response = response.render()
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/response.py" in render
109. self.content = self.rendered_content
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/response.py" in rendered_content
86. content = template.render(context, self._request)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/backends/django.py" in render
66. return self.template.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render
208. return self._render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/test/utils.py" in instrumented_test_render
94. return self.nodelist.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
174. return compiled_parent._render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/test/utils.py" in instrumented_test_render
94. return self.nodelist.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
174. return compiled_parent._render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/test/utils.py" in instrumented_test_render
94. return self.nodelist.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
70. result = block.nodelist.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
70. result = block.nodelist.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/template/library.py" in render
225. _dict = self.func(*resolved_args, **resolved_kwargs)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
339. 'results': list(results(cl))}
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/contrib/admin/templatetags/admin_list.py" in results
315. yield ResultList(None, items_for_result(cl, res, None))
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/contrib/admin/templatetags/admin_list.py" in __init__
306. super(ResultList, self).__init__(*items)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/contrib/admin/templatetags/admin_list.py" in items_for_result
217. f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/contrib/admin/utils.py" in lookup_field
290. value = attr()
File "/home/bastien/Projects/meal/venv3.5/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py" in __call__
524. manager = getattr(self.model, kwargs.pop('manager'))
Exception Type: KeyError at /admin/meal/group/
Exception Value: 'manager'
错误与ForeignKey相关的名称使用有关?如何在管理员中列出修改Group.members
(表单)?
答案 0 :(得分:0)
您无法在开箱即用的列表显示中显示相关对象的集合,这甚至会如何工作?
您可以做的是在GroupAdmin
类中提供一个方法,并返回可以放入一个更改列表表列的HTML标记(包含members
信息),并将该方法包含在{{1 }}
或者您可以在对象(list_display
)页面中使用内联。