umutbozkurt / django-rest-framework-mongoengine

Mongoengine support for Django Rest Framework
MIT License
616 stars 166 forks source link

ImportError: cannot import name MongoEngineModelSerializer #205

Closed jonasli closed 7 years ago

jonasli commented 7 years ago

I had installed the DRF-mongoengine

Successfully installed django-rest-framework-mongoengine-3.3.1

and add it to the settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
    'rest_framework',
    'rest_framework_mongoengine',
]

and in my serializers.py file , import the serializer

from rest_framework_mongoengine.serializers import MongoEngineModelSerializer

class UserSerializer(MongoEngineModelSerializer):
    class Meta:
        model = user

but I still got the error

  File "/home/jonas/django/mysite/blog/serializers.py", line 1, in <module>
    from rest_framework_mongoengine.serializers import MongoEngineModelSerializer
ImportError: cannot import name MongoEngineModelSerializer

can you please help me on this ?

thanks very much, J

BurkovBA commented 7 years ago

@jonasli

Hi! You should just from rest_framework_mongoengine import DocumentSerializer. :)

jonasli commented 7 years ago

thank you @BurkovBA .

but it doesn't work :(

File "/home/jonas/django/mysite/blog/serializers.py", line 1, in <module>
    from rest_framework_mongoengine import DocumentSerializer
ImportError: cannot import name DocumentSerializer

and I'm following the guide

https://github.com/umutbozkurt/django-rest-framework-mongoengine/blob/drf_2_support/README.md

what is wrong here ?

BurkovBA commented 7 years ago

@jonasli

Oh, I'm very sorry from rest_framework_mongoengine.serializers import DocumentSerializer.

The README, you're following is very outdated.

Try this example and this article. :)

jonasli commented 7 years ago

@BurkovBA thank you for reply.

I tried to use the routers.py in your example, just copy and paste your code, but I got the error below, do you happen to know the cause ? I appreciate .

 Performing system checks...

Unhandled exception in thread started by <function wrapper at 0xb61ebaac>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/jonas/django/mysite/mysite/urls.py", line 22, in <module>
    url(r'^blog/', include('blog.urls',namespace="blog")),
  File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py", line 50, 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/jonas/django/mysite/blog/urls.py", line 19, in <module>
    url(r'^api/',include(router.urls,namespace='api')),
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/routers.py", line 83, in urls
    self._urls = self.get_urls()
  File "/home/jonas/django/mysite/blog/routers.py", line 30, in get_urls
    urls = super(HybridRouter, self).get_urls()
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/routers.py", line 379, in get_urls
    view = self.get_api_root_view(api_urls=urls)
**TypeError: get_api_root_view() got an unexpected keyword argument 'api_urls'**
BurkovBA commented 7 years ago

@jonasli Sorry about that - this code was written for DRF 3.3.3 and they changed the router in DRF 3.4+, so now it breaks. For now just use the standard DefaultRouter from DRF-Mongoengine instead: from rest_framework_mongoengine import DefaultRouter.

jonasli commented 7 years ago

@BurkovBA thank you , I can make it run at least now. :)

G-linch commented 7 years ago

@jonasli i got the same typeerror.

but i also can not fix it. i dont konw how to change the code .

For now just use the standard DefaultRouter from DRF-Mongoengine instead: from rest_framework_mongoengine import DefaultRouter.

which py file should i change?

BurkovBA commented 7 years ago

@G-linch

Thanks for reporting this. Most probably, those are issues with changes to router in DRF 3.5. I'll have to take a closer look. In your routers.py replace

from rest_framework.routers import DefaultRouter

with

from rest_framework_mongoengine.routers import DefaultRouter

if that still doesn't work, please, let me know. :)

G-linch commented 7 years ago

I use pycharm.When I import DefaultRouter from rest_framework_mongoengine,ide canot find DefaultRouter,i search the rest_framework_mongoengine,find the DefaultRouter in routers.py,so i add the routers like this :from rest_framework_mongoengine.routers import DefaultRouter,but it still doesn't work.check the DefaultRouter from rest_framework_mongoengine.routers which show pass under the class DefaultRouter,i would follow the requirements.txt and have a try.

BurkovBA commented 7 years ago

Oh, sorry, from rest_framework_mongoengine.routers indeed.

You might also need to replace def get_qeryset(self) in your viewsets (https://github.com/BurkovBA/django-rest-framework-mongoengine-example/blob/master/project/app/views.py) with just an attribute queryset, e.g.

queryset = Tool.objects.all()

so that they look like

class ToolViewSet(MongoModelViewSet):
    """
    Contains information about inputs/outputs of a single program
    that may be used in Universe workflows.
    """
    lookup_field = 'id'
    serializer_class = ToolSerializer
    queryset = Tool.objects.all()

Or, alternatively, follow my version specification carefully in django-rest-framework-mongoengine-example and use DRF 3.3.3 (cause they've created changes to the API later).

G-linch commented 7 years ago

finally i fixed it. i pip install -r requirements.txt,and it run.it mostly the versions difference.thanks.

BurkovBA commented 7 years ago

@G-linch Great!

For newer versions of DRF, use the solution in my previous message.