DJango Models
conda activate myenv
django-admin startproject ex_models
cd ex_models
python manage.py startapp employees
Register the app -
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'employees', ] python manage.py migrate Copy paste the following in the employees/models.py
from django.db import models
class Student(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=30)
contact = models.IntegerField()
email = models.EmailField(max_length=50)
age = models.IntegerField()
Run the following commands –
python manage.py makemigrations
python manage.py migrate
Explore DB
python manage.py inspectdb
python3 manage.py dbshell
.tables
.schema employees_student
In case if you want to use MySQL – change following sections in ex_models/settings.py
DATABASES = {
'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'myproject', 'USER' : 'root', 'PASSWORD' : 'root', 'HOST' : '127.0.0.1', 'PORT' :'' } }
Run the following commands –
python manage.py makemigrations
python manage.py migrate
To verify see the DB myproject in MySQL
- Find ‘phpMyAdmin link’
- Click on the database section – and create one database named -myproject ( use encryption utf8_general_ci )