Simply define Django Models and assign them a URL route:
# models.py
from django_instant_rest.models import RestResource
class Author(RestResource):
first_name = models.CharField(max_length=255, unique=True)
last_name = models.CharField(max_length=255, null=False)
class Book(RestResource):
title = models.CharField(max_length=255)
synopsis = models.CharField(max_length= 1023)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
# urls.py
from myapp.models import *
from django_instant_rest import patterns
urlpatterns = [
patterns.resource('authors', Author),
patterns.resource('books', Book),
]
Out of the box, your REST API will have modern cursor pagination system, and complete access to Django's powerful filtering system.
// GET /authors?first_name__startswith=J&created_at__gte=2021-02-08
{
"first_cursor": "MXwyMDIxLTAyLTA5IDIwOjI2OjU3LjkyODAwMCswMDowMA==",
"last_cursor": "MXwyMDIxLTAyLTA5IDIwOjI2OjU3LjkyODAwMCswMDowMA==",
"has_next_page": false,
"data": [
{
"id": 1,
"created_at": "2021-02-09T20:26:57.928000+00:00",
"updated_at": "2021-02-09T20:26:57.928000+00:00",
"first_name": "JK",
"last_name": "Rowling"
}
]
}
All endpoints created with Django Instant Rest have a robust system of error handling.
When REST requests don't succeed, the JSON object in the response body will have a truthy errors
property.
{
"errors": [
{
"field": "last_name",
"message": "this field cannot be blank."
},
{
"field": "first_name",
"message": "author with this first name already exists."
}
]
}
Each error
object in the errors
array will have a mandatory message
property, and an optional field
property, which associates the error with a field on the resource in question.
If you're new to django-instant-rest, we recommend cloning and running the starter template