Creating models in django from scratch

Hello everyone! We are going to learn today a simple way to create a django model.
First we need to install django and create a new project. You can find all the ways to install django on the django official documentation here.
Assuming you are using python3:
$ sudo apt-get install -y python3-pip
And check it with:
$ pip3 -V
Then we need to install a virtual environment. This will allow us to create lightweight “virtual environments” with their own site directories, optionally isolated from system site directories in order to prevents conflicting packages or software from interacting with each other.
So to install virtualenv, use:
$ pip3 install virtualenv
After installation, just use the bellow command to check it:
$ virtualenv --version
We need to install Pillow in the virtual environment:
$ python -m pip install Pillow
Now we are going to install django:
mkdir django-stuff$ cd django-stuff
And we can create our virtual environment there. We can it venv by example:
$ virtualenv venv
And activate the virtual environment with:
$ . venv/bin/activate
The following commands will help us to install django in our virtual environment and create a new project:
$ pip install django$ django-admin startproject myproject
Your working directory should look like this:

With this completed, let’s create a new app:
$ django-admin startapp myapp
and the directory will be like:

Now, we can start creating models within the models.py file. But first, we need to register our new app in the settings file atthe INSTALLED_APPS section.
# myproject/myproject/settings.pyINSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp'
]
Open the models.py file and add te following code:
# myproject/myapp/models.pyfrom django.db import models...class Food(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length=20, db_index=True, null=True)
image = models.ImageField(upload_to='media', blank=True)
description = models.TextField(blank=True)
price = models.IntegerField()
def __str__(self):
return self.name
The next step is to register the models in the admin.py file in order to manage it on the django admin panel:
# myproject/myapp/admin.pyfrom django.contrib import admin
from myapp.models import Food
....admin.site.register(Food)
To complete our work, we need to run migrations wit the following commands:
$ python manage.py makemigrationsmyapp/migrations/0001_initial.py- Create model Food
and
$ python manage.py migrateOperations to perform:Apply all migrations: admin, auth, contenttypes, myapp, sessionsRunning migrations:Applying contenttypes.0001_initial... OKApplying auth.0001_initial... OKApplying admin.0001_initial... OKApplying admin.0002_logentry_remove_auto_add... OKApplying admin.0003_logentry_add_action_flag_choices... OKApplying contenttypes.0002_remove_content_type_name... OKApplying auth.0002_alter_permission_name_max_length... OKApplying auth.0003_alter_user_email_max_length... OKApplying auth.0004_alter_user_username_opts... OKApplying auth.0005_alter_user_last_login_null... OKApplying auth.0006_require_contenttypes_0002... OKApplying auth.0007_alter_validators_add_error_messages... OKApplying auth.0008_alter_user_username_max_length... OKApplying auth.0009_alter_user_last_name_max_length... OKApplying auth.0010_alter_group_name_max_length... OKApplying auth.0011_update_proxy_permissions... OKApplying myapp.0001_initial... OKApplying sessions.0001_initial... OK
The last step will be to create a superuser and access to the admin panel and check our model:
python manage.py createsuperuser
Just follow the instructions to set the username , email address and password.
And go to 127.0.0.1/admin to login after running the server with:
$ python manage.py runserver

