【问题标题】:django not reconizing default objects manager for modeldjango 无法识别模型的默认对象管理器
【发布时间】:2018-08-07 15:39:00
【问题描述】:

我正在运行我的 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

请帮忙。

拥抱和亲吻

【问题讨论】:

  • 您是否导入了 EventPurpose?尝试将 from .models import EventPurpose 放入您的views.py
  • @HenryH 是的,我导入了模型,因为我正在使用所有这些模型from models import *
  • 看起来你确实在其他地方覆盖了 EventPurpose
  • @Linovia 我彻底检查了该网站等(假设 Django 代码按预期工作)它相当令人沮丧,哈哈
  • 打印一些语句,例如 print(type(EventPurpose)) 以查看它的真正类型。可能是您定义了一个名为 EventPurpose 的视图,这将使导入无效。

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


【解决方案1】:

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

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

这就是我所做的

from models import EventPurpose as eventpurpose

【讨论】:

    猜你喜欢
    • 2011-01-24
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2012-06-26
    • 1970-01-01
    • 2023-03-04
    • 2019-11-01
    相关资源
    最近更新 更多