Manager isn't accessible via “model” instances django

六月ゝ 毕业季﹏ 提交于 2019-12-25 02:16:17

问题


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

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})

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.

Thanks in advance

EDIT: models

# 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

Traceback:

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

回答1:


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()

For this:

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

Hope helps someone



来源:https://stackoverflow.com/questions/51422987/manager-isnt-accessible-via-model-instances-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!