Django Rest framework serializers with external resource as data source

2,388 views
Skip to first unread message

Daniels Pitkevics

unread,
Apr 18, 2016, 10:21:23 AM4/18/16
to Django REST framework
Hi,

I have this situation where I dont need the basic usage of serializer - serializing model.
What I need, is to serialize external resource.
What I am trying to do:

1. My frontend app makes request to Django Rest framework API
2. It then sends request further to external API and retrieves data
3. Data should be validated and serialized with defined serializer
4. Data passed back from Django Rest framework API to frontend app.

Could anyone comment on the best possible way of achieving this?

Regards,
Daniels

Xavier Ordoquy

unread,
Apr 18, 2016, 10:44:58 AM4/18/16
to django-res...@googlegroups.com
Hi,

You’re probably looking at « regular » serializers - as opposed to ModelSerializers.
You can add your logic to call other web services there and have your serializer return the data toward your frontend.
Hope this will help.

Regards,
Xavier,
Linovia.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Daniels Pitkevics

unread,
Apr 19, 2016, 2:08:34 AM4/19/16
to Django REST framework
Hi,

Thank you for giving that tutorial. I almost successfully implemented it in my project.
One thing about tutorial is in ViewSet your example was:
serializer = serializers.TaskSerializer(instance=tasks.values(), many=True)

But, in Python 3 it should be: serializer = serializers.TaskSerializer(instance=list(tasks.values()), many=True)

All in all, great tutorial.

One thing, I couldnt manage to do, is understand, what is API Root and why my urls are not working properly. Could you give me a suggestion?

I have URL's like this:

# in app/urls.py

api_urls = [
    url(r'promotions/', include('promotions.urls', namespace='promotions')),
]

urlpatterns = [
    url(r'^api/', include(api_urls)),
]

# in promotions/urls.py

from rest_framework.routers import SimpleRouter

from .views import PromotionViewSet


router = SimpleRouter()
router.register(r'', PromotionViewSet, base_name='promotions')
urlpatterns = router.urls


But when I open in browser: http://localhost:8000/api/promotions/ it shows nothing more than empty link object in DRF response.
Although, when I change:

router.register(r'', PromotionViewSet, base_name='promotions')

to this:

router.register(r'test', PromotionViewSet, base_name='promotions')

Then I can successfully open: http://localhost:8000/api/promotions/test and see correct response.
Is there something I am missing? Probably some API Root, because in the first case, when I open the page, I see title "API Root".
I have no decorators added to any of views.

Regards, Daniels


Xavier Ordoquy

unread,
Apr 19, 2016, 4:42:52 AM4/19/16
to django-res...@googlegroups.com
Hi,

In order to fix your issue with routers, you should read the related documentation section which explains how the urls are built by routers.

Regards,
Xavier,
Linovia.

Daniels Pitkevics

unread,
Apr 19, 2016, 8:05:12 AM4/19/16
to Django REST framework
I read it, but didn't find the actual answer.
One of the options why it is not working, would be that there should be a single Router instance and I should register urls to that single router instance. 
Is that the problem?
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Xavier Ordoquy

unread,
Apr 19, 2016, 8:24:40 AM4/19/16
to django-res...@googlegroups.com
prefix is the first argument given to the register call. Therefore if you provide an empty prefix, you end up with API entry point and resource collection (or set) being at the same place, the former having precedence. Therefore you should never ever register with an empty prefix.

Note that if you want to work around this you could still drop routers and use regular urls.

Regards,
Xavier,
Linovia.


To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
signature.asc
Reply all
Reply to author
Forward
0 new messages