'module' 对象没有属性 'SortedDict' Django Tastypie 错误
'module' object has no attribute 'SortedDict' Django Tastypie error
我在 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 1.9 开始,SortedDict
已被删除。检查此 link 以供参考。
SortedDict
is deprecated as of Django 1.7 and will be removed in Django 1.9. Use collections.OrderedDict
instead. Available in Python 2.7 and 3.1+
您可以将 SortedDict
替换为 collections.OrderedDict
,如 link 中所述,方法是更改库代码和提交的合并请求中给出的一些其他更改 here.但是公平的 警告,这可能行不通,因为 Pull Request 到目前为止还没有被接受并且没有通过构建测试。
另一种选择是将 Django 降级到版本 1.8 或 1.7,直到 django-tastypie-mongoengine 可以发布适用于 Django 1.9 的稳定版本。
我在 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 1.9 开始,SortedDict
已被删除。检查此 link 以供参考。
SortedDict
is deprecated as of Django 1.7 and will be removed in Django 1.9. Use collections.OrderedDict
instead. Available in Python 2.7 and 3.1+
您可以将 SortedDict
替换为 collections.OrderedDict
,如 link 中所述,方法是更改库代码和提交的合并请求中给出的一些其他更改 here.但是公平的 警告,这可能行不通,因为 Pull Request 到目前为止还没有被接受并且没有通过构建测试。
另一种选择是将 Django 降级到版本 1.8 或 1.7,直到 django-tastypie-mongoengine 可以发布适用于 Django 1.9 的稳定版本。