我才刚刚开始学习Django框架,而我从Django 2.0开始。我创建的只是一个简单的站点,并且创建了一个自定义管理器来获取处于“已发布”状态的所有对象。但是,当我添加此管理器时,它破坏了管理员站点的“保存/编辑/删除”功能,并为我提供了错误:
Manager isn't accessible via Post instances
帖子是我为其定义了自定义管理器的对象。 (请原谅我,如果我不使用该术语)。对该错误的追溯也不是特别容易遵循。
一些希望能提供帮助的代码。
我的models.py
文件中的客户经理
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get_queryset().filter(status='published')
在我的Post班中注册经理:
class Post(models.Model):
...
objects = models.Manager()
published = PublishedManager()
tags = TaggableManager()
当我在published = PublishedManager()
行中注释时,所有功能都恢复到管理站点。自定义管理器出了什么问题?除了默认管理器之外,我还需要告诉Django在某个地方使用它吗?我当然需要更多学习,但是这些使我发疯。谢谢大家!
跟踪:
Environment:
Request Method: POST
Request URL: http://localhost:8000/admin/blog/post/add/
Django Version: 2.0.6
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'taggit']
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']
Traceback:
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
575. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
223. return view(request, *args, **kwargs)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/contrib/admin/options.py" in add_view
1554. return self.changeform_view(request, None, form_url, extra_context)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
62. return bound_func(*args, **kwargs)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
58. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/contrib/admin/options.py" in changeform_view
1451. return self._changeform_view(request, object_id, form_url, extra_context)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/contrib/admin/options.py" in _changeform_view
1483. if form.is_valid():
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/forms/forms.py" in is_valid
179. return self.is_bound and not self.errors
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/forms/forms.py" in errors
174. self.full_clean()
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/forms/forms.py" in full_clean
378. self._post_clean()
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/forms/models.py" in _post_clean
407. self.validate_unique()
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/forms/models.py" in validate_unique
416. self.instance.validate_unique(exclude=exclude)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/db/models/base.py" in validate_unique
952. date_errors = self._perform_date_checks(date_checks)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/db/models/base.py" in _perform_date_checks
1062. date = getattr(self, unique_for)
File "/Users/jlahut/Programming/Python/django-blog/venv/lib/python3.6/site-packages/django/db/models/manager.py" in __get__
178. raise AttributeError("Manager isn't accessible via %s instances" % cls.__name__)
Exception Type: AttributeError at /admin/blog/post/add/
Exception Value: Manager isn't accessible via Post instances