Django Rest Framework

A Django

refresher

Introduction

Wel­come to the sec­ond part of the series. If you missed the first part, you could read it here.

Guiding questions

I believe that these ques­tions will be a great guide to form­ing a struc­ture of under­stand­ing. Oh! there’s also a dia­gram accom­pa­ny­ing this part.

How does Django handle requests?

In views.py, we would define a url pat­tern and then spec­ify a func­tion asso­ci­ated with the pat­tern.

How does Django manage database tables?

We can think of mod­els roughly as tables. We define mod­els in mod­els.py. After we define a model, we need to make migra­tions:

poetry run python man­age.py makem­i­gra­tions

makem­i­gra­tions detect changes of the model. The changes will be gen­er­ated in the migra­tions direc­tory.
After that, we can migrate (push) changes to the data­base by exe­cut­ing:

poetry run python man­age.py migrate

How to define and compose permissions?

We gen­er­ally define per­mis­sions in per­mis­sions.py. Per­mis­sions can also be grouped by & (AND), | (OR) and ~ (NOT).

How to verify the JSON submitted from the front-end?

seri­al­iz­ers.py con­tains field con­straints. A seri­al­izer acts as a guardian of the data­base. It gov­erns how data gets in (saved to data­base) and out (sent as a JSON object).

Outro

All impor­tant points were intro­duced and lightly touched. Details will be filled as we go. Thank you for your atten­tion, and see you next time!!