using DRF with something other than models?

1,400 views
Skip to first unread message

Abraham Varricatt

unread,
Jun 20, 2016, 10:25:27 PM6/20/16
to Django REST framework
Hello,

Is it possible to use Django Rest Framework (DRF) for something other than models? Here's a trivial example,

To make an API end point which returns the current server time (in JSON format, eg: {"current_time": "XYZ"}). It can accept a string to indicate timezone, and must accept both GET and POST.

I can figure out how to do the above without DRF using regular Django urls and views. What I can't figure out is how to use DRF for the same. 

From the docs, it looks best to use routers when making a DRF end-point. Which means I need a structure like this,

urls.py <-> router <-> Viewset <-> Serializer

But as I understand Serilizers ( http://www.django-rest-framework.org/api-guide/serializers/ ), they can only be used for complex models or querysets - which is not what I need.

I feel like I'm mis-understanding something about DRF. Help please?

Puzzled,
Abraham V.

Matheus Bratfisch

unread,
Jun 20, 2016, 11:10:26 PM6/20/16
to django-res...@googlegroups.com
You could create a View that extends APIView and return directly the response on get method without Serializer.


There are ModelSerializer, ViewSet which are more related to Models, but everything is not implemented on them, they extend other classes, so you could go to the parent class and extend it.


Best regards,


--
Matheus (X-warrior) Bratfisch.
http://matbra.com

--
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.

Jeff Tchang

unread,
Jun 20, 2016, 11:15:11 PM6/20/16
to django-res...@googlegroups.com
This is how I do stuff:

in urls.py:
router.register(time', viewsets. TimeViewSet, 'time')



class TimeViewSet(viewsets.GenericViewSet):

    @list_route(methods=['get'], permission_classes=[]) 
    def timedata(self, request):

        data = {
          'current_time' : timezone.now()
        }
        return Response(data)        

Obviously better to do stuff in a RESTful manner but this works for me.

-Jeff

Info Cascade

unread,
Jun 20, 2016, 11:51:33 PM6/20/16
to django-res...@googlegroups.com
I think something like this?

urls.py
import views
urlpatterns = [
url(r'^server_time', views.ServerTimeView.as_view(),
name='server_time'),
]

views.py
import datetime
from rest_framework.views import APIView
from rest_framework.response import Response

class ServerTimeView(APIView):
def get(self, request, format=None):
return Response({
'server_time': datetime.datetime.utcnow()
})
def post(self, request, format=None):
try:
new_time =
datetime.datetime.strptime(request.data['server_time'],"%Y-%m-%dT%H:%M:%S")
# set the system time to "new_time"

return Response({
'response': new_time
})
except:
pass

curl -i --request POST -H "Content-type: application/json" --data
'{"server_time":"2016-06-21T03:31:45"}'
http://192.168.1.120:8000/api/server_time

I don't think you need serializers.
> --
> 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
> <mailto:django-rest-fram...@googlegroups.com>.

Xavier Ordoquy

unread,
Jun 21, 2016, 3:42:32 AM6/21/16
to django-res...@googlegroups.com
Hi,

It explains how to use a Viewset without models.

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.
signature.asc

Tom Christie

unread,
Jun 21, 2016, 9:47:23 AM6/21/16
to Django REST framework
I'd really recommend just using regular views and URL conf. Writing views explicitly, using renderers/parsers/permissions/authentication to set up your basic API policies, making use of serializers for data validation, etc... gives you plenty of value over using regular Django, while still keeping your view logic nice and explict.

You *can* use routers and viewsets, but it's typically over-complicated, unless you happen to be writing a CRUD-style API.

Cheers,

  Tom
Reply all
Reply to author
Forward
0 new messages