Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,66 @@ If you need a more fine graned authentication you could check your current endpo
return self.request.user.is_authenticated()
else:
return True


Example with Django
===================

This is an example of an endpoint for a model Book::

from restless.dj import DjangoResource
from restless.preparers import FieldsPreparer

from .models import Book

class BookResource(DjangoResource):
preparer = FieldsPreparer(fields={
'book_author': 'author.name',
'book_title': 'title',
'book_pages': 'pages',
})

def list(self):
return Book.objects.all()

def detail(self, pk):
return Book.objects.get(id=pk)

def create(self):
return Book.objects.create(
author=Author.objects.get(name=self.data['author']),
title=self.data['title'],
pages=self.data['pages']
)

def update(self, pk):
book = Book.objects.get(id=pk)
book.author = Author.objects.get(name=self.data['author'])
book.title = self.data['title']
book.pages = self.data['pages']
book.save
return book


# On the urls.py...
from django.conf.urls import include, path
from .api import BookResource

urlpatterns = [
path(r'books/', include(BookResource.urls())),
]


Using serializer
================

How to use restless without preparers. The methods must return a dict::

from restless.resources import Resource
from restless.serializers import JSONSerializer

class MyResource(Resource):
serializer = JSONSerializer()

def detail(self, pk):
return {'pk': pk}