django没有为模型重新定义默认对象管理器

时间:2018-02-27 21:12:55

标签: python django django-rest-framework models attributeerror

我正在运行我的django服务器,我收到错误

的500代码

AttributeError: type object 'EventPurpose' has no attribute 'objects'

然而,模型是正确创建的,具有相同实现的其他模型工作正常。以下是要使用的模型创建的完整代码。

模型创建

class EventPurpose(models.Model):
    purpose = models.CharField(max_length=200)

视图

class GetAllSettingsObjects(APIView):

    def get(self, request, format=None):
        eventpurposequeryset = EventPurpose.objects.all()

那就是我得到错误:错误完整

line 24, in get
    eventpurposequeryset = EventPurpose.objects.all()
AttributeError: type object 'EventPurpose' has no attribute 'objects'
[27/Feb/2018 20:57:55] "GET /<super cool url object that I dont want to share with you>/ HTTP/1.1" 500 16404

我仍然没有想到这一点,所以这里有完整的追溯:

Internal Server Error: /api/dependancy/suitsadmin/settings/
Traceback (most recent call last):
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/rest_framework/views.py", line 494, in dispatch
    response = self.handle_exception(exc)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/rest_framework/views.py", line 454, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/local/lib/python2.7/site-packages/rest_framework/views.py", line 491, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/rickus/Documents/softwareProjects/211hospitality/suitsandtables/backend/virtualsuits/suitsandtables/suitsandtablessettingsapp/views.py", line 24, in get
    eventpurposequeryset = EventPurpose.objects.all()
AttributeError: type object 'EventPurpose' has no attribute 'objects'
[28/Feb/2018 00:52:12] "GET /api/dependancy/suitsadmin/settings/ HTTP/1.1" 500 19430

请帮忙。

拥抱和亲吻

1 个答案:

答案 0 :(得分:0)

所以我找到了解决方案。这对我来说毫无意义,但是我将EventPurpose再次归结为eventpurpose再次不知道为什么这会起作用并且会喜欢一个理由。

我从这个问题得到了答案:type object 'X' has no attribute 'objects'

这就是我做的事情

from models import EventPurpose as eventpurpose