访问经理实例django

访问经理实例django

本文介绍了无法通过“模型"访问经理实例django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

django及其自定义管理器出现错误.我有这个自定义管理器:

I am suffering an error with django and their custom Managers. I have this custom Manager:

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(通话/v​​iews.py)中检索数据

Ok, So now I am trying to retrive data from 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显示以下消息:

When Call.objects.all() is executed, django shows this message:

Manager isn't accessible via Call instances

我不知道这里发生了什么.

I have no idea about what's happening here.

预先感谢

模型

# 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

在处理上述异常期间,发生了另一个异常:

During handling of the above exception, another exception occurred:

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

推荐答案

解决方案:看来这是我模型中的字段与经理之间的冲突.该字段被命名为活动".然后我改变了这个:

Solution:It seems It was conflict between a field in my model and the manager.The field was named as "active".Then I changed this:

objects = models.Manager()
active = CallManager()

为此:

objects = models.Manager() # Default manager
objects_active = CallManager() # Custom manager

希望可以帮助某人

这篇关于无法通过“模型"访问经理实例django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:00