【问题标题】:'module' object has no attribute 'SortedDict' Django Tastypie error'module' 对象没有属性 'SortedDict' Django Tastypie 错误
【发布时间】:2016-07-22 19:28:24
【问题描述】:

我在 Django 中使用 tastypie_mongoengine 作为 REST Api。

models.py

import mongoengine
import datetime

class Students(mongoengine.Document):
    name = mongoengine.StringField(required=True)
    age = mongoengine.StringField(required=True)
    student_class = mongoengine.StringField(required=True)`

api.py

from tastypie import authorization
from tastypie_mongoengine import resources
from models import Students

class StudentsResource(resources.MongoEngineResource):
    class Meta:
        queryset = Students.objects.all()
        allowed_methods = ('get', 'post', 'put','delete', 'patch')
        authorization = authorization.Authorization()

我收到以下错误:

  File "/home/my_name/projects/StudentBehaviour/mysite/mysite/urls.py", line 3, in <module>
    from app.api import StudentsResource
  File "/home/my_name/projects/StudentBehaviour/mysite/app/api.py", line 3, in <module>
    from tastypie_mongoengine import resources
  File "/home/my_name/projects/StudentBehaviour/env/local/lib/python2.7/site-packages/tastypie_mongoengine/resources.py", line 54, in <module>
    class ListQuerySet(datastructures.SortedDict):
AttributeError: 'module' object has no attribute 'SortedDict'

我该如何解决这个问题?

【问题讨论】:

  • 你使用的是哪个版本的 Django?
  • Django==1.9.4 django-tastypie==0.13.3 django-tastypie-mongoengine==0.4.6 mongoengine==0.8.1

标签: python django rest tastypie mongoengine


【解决方案1】:

嗯,从 Django 1.9 开始,SortedDict 已被删除。检查此link 以供参考。

SortedDict 自 Django 1.7 起已弃用,并将在 Django 1.9 中删除。请改用​collections.OrderedDict。在 Python 2.7 和 3.1+ 中可用

您可以将SortedDict 替换为链接中提到的​collections.OrderedDict,方法是更改​​库代码以及提交的此拉取请求中给出的一些其他更改here。但是公平的警告,这可能不起作用,因为 Pull Request 到目前为止未被接受并且未通过构建测试。

另一种选择是将 Django 降级到 1.8 或 1.7 版本,直到 django-tastypie-mongoengine 可以发布适用于 Django 1.9 的稳定版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 2014-08-15
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多