MattBroach / DjangoRestMultipleModels

View (and mixin) for serializing multiple models or querysets in Django Rest Framework
MIT License
549 stars 67 forks source link

How it work ? if you can make a simple example @MattBroach #27

Closed MohamedShawky closed 7 years ago

MattBroach commented 7 years ago

Hey @MohamedShawky -- I'm not sure there's a simpler example than the one in the README. Is there a particular question that I can help you with?

MohamedShawky commented 7 years ago

Hey @MattBroach i follow the steps but it didnot work please help me thank you in advance

MattBroach commented 7 years ago

It's hard to diagnose the problem without any information. Generally, when posting about a problem like this it's useful to provide:

  1. The code you used in your app that isn't working
  2. The traceback from the error, so we can see exactly what went wrong.
MohamedShawky commented 7 years ago

ok @MattBroach in viewset `class TextAPIView(MultipleModelAPIView): queryList = [ (User.objects.all(), UserSerializer), (Comment.objects.filter(style='Sonnet'), CommentSerializer),

 ]`

and in urls.py router = Router.SimlpeRouter() router.register('text', TextAPIView.as_view(), base_name='text') when i run server and visit url it didnot work

and this traceback

Environment:

Request Method: GET Request URL: http://127.0.0.1:8000/textNested/

Django Version: 1.9 Python Version: 3.4.2 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework_mongoengine', 'rest_framework', 'drf_multiple_model', 'mongoengine', 'api', 'Jmodel'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response

  1. response = response.render()

File "C:\Python34\lib\site-packages\django\template\response.py" in render

  1. self.content = self.rendered_content

File "C:\Python34\lib\site-packages\rest_framework\response.py" in rendered_content

  1. ret = renderer.render(self.data, media_type, context)

File "C:\Python34\lib\site-packages\rest_framework\renderers.py" in render

  1. context = self.get_context(data, accepted_media_type, renderer_context)

File "C:\Python34\lib\site-packages\rest_framework\renderers.py" in get_context

  1. raw_data_post_form = self.get_raw_data_form(data, view, 'POST', request)

File "C:\Python34\lib\site-packages\rest_framework\renderers.py" in get_raw_data_form

  1. serializer = view.get_serializer()

File "C:\Python34\lib\site-packages\rest_framework\generics.py" in get_serializer

  1. serializer_class = self.get_serializer_class()

File "C:\Python34\lib\site-packages\rest_framework\generics.py" in get_serializer_class

  1. % self.class.name

Exception Type: AssertionError at /textNested/ Exception Value: 'NsetedSerializerView' should either include a serializer_class attribute, or override the get_serializer_class() method.

MattBroach commented 7 years ago

Hmm, so I see a lot of possible problems here, but I don't think that error is actually being caused by the MultipleModelAPIView. Here are a few things to think about:

  1. The Traceback is showing the error on NsetedSerializerView, but the MultipleModelAPIView is being used in a class called TextAPIView.
  2. The URL you are accessing is /textNested/, but your router base_name is just text. It is likely accessed at something like /text/ or /api/text/ (although it's impossible to tell without seeing the full url list)
  3. Although it isn't causing the problem, DRF's routers are really meant to be used with Viewsets, not Views. If you want to use the Django Rest Multiple Model package with routers, you should use the included MultipleModelAPIViewset or the MultipleModelMixin

Good luck.