'模块'对象没有属性' SortedDict' Django Tastypie错误

时间:2016-04-03 07:12:21

标签: python django rest tastypie mongoengine

我在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'

如何解决此问题?

1 个答案:

答案 0 :(得分:2)

好吧,从Django 1.9开始,SortedDict已被删除。请查看此link以供参考。

  从Django 1.7开始,

SortedDict已弃用,将在Django 1.9中删除。请改用collections.OrderedDict。适用于Python 2.7和3.1 +

您可以通过更改库代码以及此提交的提交请求here中提供的其他更改,将SortedDict替换为链接中提到的​collections.OrderedDict。但是公平的警告,这可能不起作用,因为到目前为止还没有接受Pull Request并且构建测试失败了。

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