61 Followers
·
Follow

Image for post
Image for post

Introduction

The article introduces how to use DRF-Nested-Routers framework https://github.com/alanjds/drf-nested-routers to create URLs for nested resources. In this tutorial, our target is to create APIs for getting libraries and books.

/api/libraries/ -> Get list of libraries
/api/libraries/{library_id}/books/ -> Get list of book in a library
/api/libraries/{library_id}/books/{book_id} -> Get details of a book

Diagram

Image for post
Image for post
The ERD of the project

Problem

Django Rest Framework supports to generate the URL by using Routers.

REST framework adds support for automatic URL routing to Django, and provides you with a simple, quick and consistent way of wiring your view logic to a set of URLs.

Here’s an example of a simple URF conf using SimpleRouter according to Django Rest Framework document. …


Image for post
Image for post

Introduction

In Django, to work with the legacy database, you must set the manged=Falsein the ORM model Meta class to Django know that you don’t want to create, modify or delete the table. For example, the Person class below tells Django that it already has a table called ‘APP_PERSON’ in the database.

class Person(models.Model):
id = models.IntegerField(primary_key=True)
first_name = models.CharField(max_length=70)
class Meta:
managed = False
db_table = 'APP_PERSON'

However, it becomes a challenge for unit testing, because Django automatically generates the testing database (blank database) which we may not have the SQL script to create it. …

About

Thuc Pham

Senior iOS Developer, lives in Melbourne

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store