无法通过 "model" 个实例访问管理器 django
Manager isn't accessible via "model" instances django
我遇到了 django 及其自定义管理器的错误。我有这个自定义管理器:
class CallManager(models.Manager):
def get_queryset(self):
return super(CallManager, self).get_queryset().filter(is_active=True)
class Call(models.Model):
...
# Data
is_active = models.BooleanField(default=True)
# Managers
objects = models.Manager() # Default
active = CallManager() # Active calls
好的,现在我正在尝试从 views.py (call/views.py)
中检索数据
# Call list
def call_list(request):
list = Call.objects.all() # Error here
render(request, 'call/call_list.html', {'list': list})
当 Call.objects.all() 被执行时,django 显示这个消息:
Manager isn't accessible via Call instances
我不知道这里发生了什么。
提前致谢
编辑:模型
# Relations
#country = models.ManyToManyField...
individual = models.ManyToManyField...
project = models.ForeignKey(Project, on_delete=models.CASCADE)
criteria = models.ManyToManyField...
# Data
call_name = models.CharField(max_length=20, default=None)
start_date = models.DateField(default=None)
end_date = models.DateField(default=None, null=True, blank=True)
is_active = models.BooleanField(default=True)
# Creation
created_at = models.DateTimeField(auto_now_add=True, null=True)
modified_at = models.DateTimeField(auto_now=True, blank=True)
# Managers
objects = models.Manager() # Default
active = CallManager() # Active calls
回溯:
TypeError: 'Call' object is not subscriptable
在处理上述异常的过程中,又发生了一个异常:
Traceback (most recent call last):
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "path...virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "path.../call/views.py", line 27, in call_list
return render(request, 'call/call_list.html', {'list': list})
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 175, in render
return self._render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/test/utils.py", line 98, in instrumented_test_render
return self.nodelist.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/test/utils.py", line 98, in instrumented_test_render
return self.nodelist.render(context)
File "path...virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "/Users/Jotadrive/.virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/defaulttags.py", line 211, in render
nodelist.append(node.render_annotated(context))
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 993, in render
output = self.filter_expression.resolve(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 676, in resolve
obj = self.var.resolve(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 802, in resolve
value = self._resolve_lookup(context)
File "path...virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 843, in _resolve_lookup
current = getattr(current, bit)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/db/models/manager.py", line 178, in __get__
raise AttributeError("Manager isn't accessible via %s instances" % cls.__name__)
AttributeError: Manager isn't accessible via Call instances
[19/Jul/2018 18:13:12] "GET /call/list/ HTTP/1.1" 500 200797
解决方法:
看来这是我模型中的一个字段与经理之间的冲突。
该字段被命名为 "active"。
然后我改变了这个:
objects = models.Manager()
active = CallManager()
为此:
objects = models.Manager() # Default manager
objects_active = CallManager() # Custom manager
希望对某人有所帮助
我所做的是这个(我用 django shell 做了,所以你可以看到它有效):
首先我打开了 Django shell
py manage.py shell
然后您需要从应用程序调用您的模型。
>>> from <app_name>.models import <model_name>
>>> querydata = <model_name>.objects.all()
>>> querydata
<QuerySet [<<mode_name>: randomdata>, <<mode_name>: randomdata2>]>
希望我说清楚了。
我遇到了 django 及其自定义管理器的错误。我有这个自定义管理器:
class CallManager(models.Manager):
def get_queryset(self):
return super(CallManager, self).get_queryset().filter(is_active=True)
class Call(models.Model):
...
# Data
is_active = models.BooleanField(default=True)
# Managers
objects = models.Manager() # Default
active = CallManager() # Active calls
好的,现在我正在尝试从 views.py (call/views.py)
中检索数据# Call list
def call_list(request):
list = Call.objects.all() # Error here
render(request, 'call/call_list.html', {'list': list})
当 Call.objects.all() 被执行时,django 显示这个消息:
Manager isn't accessible via Call instances
我不知道这里发生了什么。
提前致谢
编辑:模型
# Relations
#country = models.ManyToManyField...
individual = models.ManyToManyField...
project = models.ForeignKey(Project, on_delete=models.CASCADE)
criteria = models.ManyToManyField...
# Data
call_name = models.CharField(max_length=20, default=None)
start_date = models.DateField(default=None)
end_date = models.DateField(default=None, null=True, blank=True)
is_active = models.BooleanField(default=True)
# Creation
created_at = models.DateTimeField(auto_now_add=True, null=True)
modified_at = models.DateTimeField(auto_now=True, blank=True)
# Managers
objects = models.Manager() # Default
active = CallManager() # Active calls
回溯:
TypeError: 'Call' object is not subscriptable
在处理上述异常的过程中,又发生了一个异常:
Traceback (most recent call last):
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "path...virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "path.../call/views.py", line 27, in call_list
return render(request, 'call/call_list.html', {'list': list})
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 175, in render
return self._render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/test/utils.py", line 98, in instrumented_test_render
return self.nodelist.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/test/utils.py", line 98, in instrumented_test_render
return self.nodelist.render(context)
File "path...virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "/Users/Jotadrive/.virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 943, in render
bit = node.render_annotated(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/defaulttags.py", line 211, in render
nodelist.append(node.render_annotated(context))
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 910, in render_annotated
return self.render(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 993, in render
output = self.filter_expression.resolve(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 676, in resolve
obj = self.var.resolve(context)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 802, in resolve
value = self._resolve_lookup(context)
File "path...virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/template/base.py", line 843, in _resolve_lookup
current = getattr(current, bit)
File "path....virtualenvs/apolo_venviorment/lib/python3.6/site-packages/django/db/models/manager.py", line 178, in __get__
raise AttributeError("Manager isn't accessible via %s instances" % cls.__name__)
AttributeError: Manager isn't accessible via Call instances
[19/Jul/2018 18:13:12] "GET /call/list/ HTTP/1.1" 500 200797
解决方法: 看来这是我模型中的一个字段与经理之间的冲突。 该字段被命名为 "active"。 然后我改变了这个:
objects = models.Manager()
active = CallManager()
为此:
objects = models.Manager() # Default manager
objects_active = CallManager() # Custom manager
希望对某人有所帮助
我所做的是这个(我用 django shell 做了,所以你可以看到它有效): 首先我打开了 Django shell
py manage.py shell
然后您需要从应用程序调用您的模型。
>>> from <app_name>.models import <model_name>
>>> querydata = <model_name>.objects.all()
>>> querydata
<QuerySet [<<mode_name>: randomdata>, <<mode_name>: randomdata2>]>
希望我说清楚了。