In this repository, I followed a Django full tutorial from the official site. Here is the link: https://www.django-rest-framework.org/tutorial/1-serialization/
This README provides the log what I've done with this repository. This repository also the result of my study in that tutorial.
Table of Content
- Initial Steps
- Reconfigure the project
- Models and Migrations
- Serializer Class
- Model Serializer
- Regular Djago Views using our Serializer
The initial steps is the same as my quickstart repository.
Let's start this learn by creating your virtual environment (venv). Please refer to this link to learn how to make one: https://gist.github.com/ryumada/c22133988fd1c22a66e4ed1b23eca233
Install these packages below using this command:
pip install package-name
Required Packages
- Python (3.6, 3.7, 3.8, 3.9, 3.10)
- Django (2.2, 3.0, 3.1, 3.2, 4.0)
- DjangoRESTframework
Optional Packages:
- PyYAML, uritemplate (5.1+, 3.0.0+) - Schema generation support.
- Markdown (3.0.0+) - Markdown support for the browsable API.
- Pygments (2.4.0+) - Add syntax highlighting to Markdown processing.
- django-filter (1.0.1+) - Filtering support.
- django-guardian (1.1.1+) - Object level permissions support.
create a project called tutorial, with this command:
django-admin startproject tutorial .
In this repository I'm not using the last dot (.) syntax, so it'll create the project inside tutorial directory. But in the future, you should create the project inside the root repository.
Go inside the project directory.
cd tutorial
Then create an app called quickstart.
django-admin startapp quickstart
Then get back to tutorial's directory.
cd ..
python3 manage.py migrate
python3 manage.py createsuperuser --email admin@example.com --username admin
If you have clone a djangorestframework project, and want to continue the development. You can do these initial steps instead:
Let's start this learn by creating your virtual environment (venv). Please refer to this link to see how to make it: https://gist.github.com/ryumada/c22133988fd1c22a66e4ed1b23eca233
pip install -r requirement.txt
django-admin startapp app-name
Register the package you want to use and the application you've created on settings.py
inside the _main
directory. Then find the INSTALLED_APPS
variable and insert the package name as the value.
python3 manage.py migrate
python3 manage.py createsuperuser --email admin@example.com --username admin
Create a model. This model is used to create a table in the database. The model I created is in: snippets/models.py
We use makemigrations
and migrate
. We will run this command to make the migration and migrate it:
python manage.py makemigrations snippets # this will create a migration file (snippets/migrations/0001_initial.py), the migration file generated from snippets/models.py
python manage.py migrate snippets # this will run the migration file
The documentation says that the Serializer is like Django form class. It creates a structure of data, provides a common data validation, and controls how the serializer should be displayed (such as {'base_template': textarea.html}
).
The path for a serializer I create
snippets/serializers.py
ModelSerializer
creates a shortcut to create serializer from a model. It set:
- An automatically determined set of fields.
- Simple default implementation for the
create()
andupdate()
methods.
This will create API interface where we can access it in curl or web browser.
- I create a snippet views in
snippets/views.py
- wire the views (routing) by creating
snippets/urls.py
- wire up the root urlcof (routing) in the
tutorial/urls.py
, to include our snippet app's URLs.
PS: See commit changes to learn more about this repository