Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Simple REST with Django

Simple REST with Django

An overview of history and current state of REST support in Django ecosystem, including what's great in Django Rest Framework but also when it might be a good idea to use something else.

Senko Rašić

December 09, 2014

More Decks by Senko Rašić

Other Decks in Programming


  1. Do you have a minute? ! I’d like to talk

    to you about our Lord and Saviour
  2. ..meanwhile in Django.. from django.http import JSONResponse from django.core import

    serializers ! ! def books_list(request): qs = Book.objects.all() return JSONResponse(qs.values()) ! ! def book_detail(request, book_id): book = get_object_or_404(Book, id=book_id) dct = serializers.serialize(‘python’, [book]) return JSONResponse(dct)
  3. DjangoRestless from restless.views import Endpoint from restless.models import serialize !

    class BookList(Endpoint): def get(self, request): return serialize(Book.objects.all()) ! class BookDetail(Endpoint): def get(self, request, book_id): book = get_object_or_404(Book, id=book_id) return serialize(book)
  4. model views from restless.modelviews import * ! ! class BookList(ListEndpoint):

    model = Book ! class BookDetail(DetailEndpoint): model = Book
  5. custom serialization from restless.models import serialize ! ! def my_ordinary_view(request):

    book = Book.objects.all() return serialize(book, fields=[ ‘title’, (‘author’, { fields=[‘name’, ‘address’]}), (‘publisher’, { fields=[‘name’, ‘address’]}), ‘summary’, (’n_comments’, lambda x: x.comments.Count())])
  6. ..and now for the conclusion DRF is awesome ! but

    ! Use the right tool for the job