Django RESTful Framework

What is MVT?

Model: Data structuring

View: Where data flows

Template: Where data is displayed

What is API?

  • Application Programming Interface

    • Developer interface

      • Developers only need Data!

Request

: Requests are sent via URL!

Data notation

: Convention

  • JSON

    • JavaScript Object Notation

    • Javascript object-style notation

  • XML

    • eXtended Markup Language (W3C, 1996)

Why not HTML?

: Key values are not reflected!

  • That's why XML was introduced, allowing us to define tags as we wish

Why JSON

  • XML is longer due to closing tags

    • Money

What we need to do

: In Django, provide only data in JSON format!

JSON ... and then?

JSON Concept

Reasons for separating Javascript & framework

  1. For better user experience

    • Bad UX -> No Users -> No Money

    • data -> What do humans like?

    • Mobile applications (web)

    • churn (attrition rate)

    • JS essential (Adobe Flash)

  2. Because separation is convenient

Reinstalling Django

Using Faker

Install faker

Create dummy data

RESTful API

Reference: https://meetup.toast.com/posts/92arrow-up-right

: A way to organize URLs neatly (common rules / conventions)

RESTful

  1. HTTP verb (GET, POST)

  2. Composed of nouns (plural form)

Rules

  • Don't put verbs in URL! -> Use HTTP method

    • C (POST)

      • (POST) / articles /

    • R (GET)

      • index (all information) - (GET) / articles /

      • detail (one information) - (GET) / articles / <id>

    • U (PUT/PATCH)

      • (PUT) / articles / <id>

    • D (DELETE)

      • (DELETE) / articles / <id>

  • Only put objects in URL -> In plural form

    • Data

  1. subdomain

    • ex)

      • lab.ssafy.com

      • api.github.com

  2. Separated URL /api/

    • ssafy.com/api/lectures/

    • github.com/api/repos/

  3. versioning

    • ssafy.com/api/v1/lectures/

    • POST /api/articles/1/like/

    • POST /api/articles/1/comments/like/

Django REST Framework (DRF)

Install djangorestframework

Check if installed

Serialize (Serialization)

Format conversion (data transmission/movement)

dict -> JSON (stringify, serialize)

JSON -> dict (parse, deserialize)

Serialization

: Object(language, database) -> String (JSON)

CREATE

Create Process

Pretty error output with raise_exception

ex)

Send only one

Single Response

Error message

Error Message

yasg

  • Automatically generates API related documentation

Install DRF yasg

https://drf-yasg.readthedocs.io/en/stable/readme.htmlarrow-up-right

Loading dummy data as JSON

Put dummy.json in fixtures folder

Put dummy.json into DB with loaddata

Dump data from DB with dumpdata

Create JSON file from dumped data with dumpdata

This way they stick together

Make it pretty with indenting

--indent 2 -> Give indenting of 2

result

Last updated