python - 类型对象 'ModelDeclarativeMetaclass' 没有属性 'Meta'

标签 python django tastypie

我已经在 django 中为一个模型创建了资源并在 urls.py 中注册了 但是当我访问主页时出现以下错误。

错误栈:

ERROR: Internal server error: /
Traceback (most recent call last):
  File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 98, in get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 338, in resolve
    for pattern in self.url_patterns:
  File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 367, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 361, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/himanshu_office/projects/NDSE/NDSE/urls.py", line 28, in <module>
    url(r'^questionnaire/', include('questionnaire.urls', namespace='questionnaire')),
  File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 28, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/himanshu_office/projects/NDSE/NDSE/questionnaire/urls.py", line 8, in <module>
    questionnaire_api_v1.register(questionnaire_resources.SectionResource)
  File "/home/himanshu_office/Envs/ndse/local/lib/python2.7/site-packages/tastypie/api.py", line 54, in register
    resource.__class__.Meta.api_name = self.api_name
AttributeError: type object 'ModelDeclarativeMetaclass' has no attribute 'Meta'

文件:问卷/api/resources.py

from tastypie import resources, fields
from tastypie.authorization import Authorization
from questionnaire import models as questionnaire_models


class SectionResource(resources.ModelResource):
    """
    Resource for Section model with deepest level
    """

    class Meta:
        resource_name = 'section'
        queryset = questionnaire_models.Section.objects.all()
        list_allowed_methods = ['get']
        detail_allowed_methods = ['get']
        authorization = Authorization()
        always_return_data = True
        include_resource_uri = True
        fields = ['name']

文件:问卷/models.py

from jsonfield import JSONField
from sanitizer.models import SanitizedTextField

from django.db import models

from evaluation import models as evaluation_models


class Section(models.Model):
    """
    Model to Store Section which represent collection of questions
    """
    name = models.CharField(max_length=100, unique=True,
        help_text="Unique name of section to classify questions")
    def __unicode__(self):
        return self.name

文件:问卷/urls.py

from django.conf.urls import patterns, url, include

from tastypie.api import Api

from questionnaire.api import resources as questionnaire_resources

questionnaire_api_v1 = Api(api_name='questionnaire')
questionnaire_api_v1.register(questionnaire_resources.SectionResource)

urlpatterns = patterns('',
    url('^', include(questionnaire_api_v1.urls)),
)

最佳答案

注册api时出错 应该是

questionnaire_api_v1.register(questionnaire_resources.SectionResource())

代替:

questionnaire_api_v1.register(questionnaire_resources.SectionResource)

关于python - 类型对象 'ModelDeclarativeMetaclass' 没有属性 'Meta',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31610312/

相关文章:

python - 在Python中使用For循环创建一个xml文件

django - 当部署 Django 应用程序 (`python manage.py collectstatic` 不起作用时,如何向 AWS 提供静态文件)?

python - tastypie 从非 pk 字段访问数据

python - 在迭代 csv 文件的行时,动态地将计算列添加到 pandas 数据框?

python - os.listdir 正在删除字符重音

python - 如何在 Kivy 中运行 Selenium(浏览器在 Kivy 窗口中打开)

python - 如何在Django Rest框架中的manytomanyfield中的序列化器中获取键的值?

django - 在 Django 中放置常见查询的位置?

python - Tastypie 嵌套资源 - cached_obj_get() 正好接受 2 个参数(给定 1 个)

python - Django - Tastypie POST(更新)导致 409 CONFLICT