Sign in
Log inSign up
Django Rest Framework Part 1:Project and Model.

Django Rest Framework Part 1:Project and Model.

Caleb cheptumo's photo
Caleb cheptumo
·Feb 26, 2022·

2 min read

Prerequisites

Computer/laptop with windows OS installed. Python Installed. Visual studio Code. Web Browser – Chrome.

Hello Developers, welcome to the Learn Django Rest Framework Series. In this tutorial will walk you through how to create a Django Rest Framework project and create a model. I am going to show you step by step from zero to advance.

Setting Up Virtual Environment

Virtual environments are a great tool for those who want to develop outside of the default Python installation. They are useful for testing out packages before installing them globally, preventing conflicts with global libraries, and creating isolated development environments. Setting up a virtual environment with Python is quick and easy.

Installation

pip install virtualenv

First lets create a folder Django Rest Framework . Navigate to the directory and then create virtual environment using:

virtualenv venv

Activate the environment to install packages.

venv/Scripts/activate

Django will be the first package we will install.

pip install Django

Second is to install django rest framework.

pip install djangorestframework

Check the package installed .

pip list

Create news project. After successful installation , create django project named news, using django-admin.

django-admin startproject news

Navigate to the project directory , and create django app named content.

python manage.py startapp content

Add installed apps in the settings.py under INSTALLED_APPS

Run server

python manage.py runserver

Visit the given URL: 127.0.0.1:8000

You should be able to see The install worked successfully! Congratulations!

Create a model.

Now we are going to create a model under content app.

auto_now_add automatically saves time when an article is created which only happens once , on the other hand auto_now automatically saves the date when any change is made on the article .

Register the model.

Register the model created in the admin.py to be visible in the admin panel.

Start by importing the model from content.

Make migrations and migrate.

First you need to initiate the migration using

python manage.py makemigrations

Which should give the following output:

Migrations for 'content':

content\migrations\0001_initial.py
    - Create model Article

After initiating we the synchronize our database using.

python manage.py migrate

Create superuser

Superuser is used to manage the admin site . Create using:

python manage.py createsuperuser

Complete by entering your desire username, email, and password.

Runserver to access the admin site using the URL > 127.0.0.1:8000/admin

That’s all for part one, In the next tutorial we will create our first serializer hope you liked.

https://calebcheptumo.com/django-rest-framework-part-1-project-and-model/