Search code examples
pythondjangoresttastypiemongoengine

'module' object has no attribute 'SortedDict' Django Tastypie error


I am using tastypie_mongoengine for the REST Api in Django.

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()

I am getting the following error :

  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'

How do I resolve this issue?


Solution

  • Well, from Django 1.9 onwards, SortedDict has been removed. Check this link for reference.

    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+

    You can replace SortedDict with ​collections.OrderedDict as mentioned in the link by making changes to the library code and some other changes as given in this Pull Request submitted here. But fair warning, this might not work as the Pull Request was not accepted so far and has failed the build test.

    Another option is to downgrade Django to either version 1.8 or 1.7 till django-tastypie-mongoengine can release a stable version which will work for Django 1.9.