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.

382b18eebb1386e80ff7e20fd1eb784e?s=128

Senko Rašić

December 09, 2014
Tweet

Transcript

  1. Simple REST with Django Senko Rašić, Dobar Kod

  2. Previously in Django… Django Piston ! TastyPie ! roll your

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

    to you about our Lord and Saviour
  4. Django REST Framework Browsable API ! Authentication ! Serialization !

    Docs & community
  5. ..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)
  6. 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)
  7. model views from restless.modelviews import * ! ! class BookList(ListEndpoint):

    model = Book ! class BookDetail(DetailEndpoint): model = Book
  8. 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())])
  9. ..and now for the conclusion DRF is awesome ! but

    ! Use the right tool for the job