To develop a Django application to store and retrieve data from a database using Object Relational Mapping(ORM).
-
Creating Repository:
- First, a path to make a folder where git needs to be created is identified.
- Fork the repository (https://github/gowriganeshns/django-orm-app)
- Clone the repository
git clone https://github.com/Nijeesh-bit/django-orm-app - After cloning the folder with the repository name django-orm-app will be created
-
Inside django-orm-app:
- Write the following commands
cd django-orm-appdjango-admin startproj myproj- Then move into the folder myproj where manage.py file is located. Now give the commands to create myapp
python3 manage.py startapp myapp- Then change the necessary settings in the settings.py.
from pathlib import Path import osALLOWED_HOSTS = ['*']INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp' ]STATICFILES_DIR=[ os.path.join(BASE_DIR,'static') ] -
Create a Super User:
``` python3 manage.py makemigrations python3 manage.py migrate python3 manage.py createsuperuser ```- Enter the admin and password according to your preferences.
- You will receive the command Superuser successfully created.
- Visit the admin app at (http://127.0.0.1:8000/admin) and log in with the superuser account that you have created:
-
Write the code: - Write the given codes in models.py and admin.py
-
Run the code: - Make migrations
python3 manage.py makemigrations myapp python3 manage.py migrate myapp- In the admin interface (http://127.0.0.1:8000/admin) following web interface will be obtained. - Now add 10 employee details having only 5 post.
models.py
from django.db import models
from django.contrib import admin
class Student(models.Model):
referencenumber=models.CharField(max_length=20,help_text="refernce number")
name=models.CharField(max_length=100)
age=models.IntegerField()
email=models.EmailField()
phonenumber=models.IntegerField()
class StudentAdmin(admin.ModelAdmin):
list_display=('referencenumber','name','age','email','phonenumber')
class Employee (models.Model):
emp_id=models.CharField(primary_key=True,max_length=4,help_text='Employee ID')
ename=models.CharField(max_length=50)
post=models.CharField(max_length=20)
salary=models.IntegerField()
class EmployeeAdmin(admin.ModelAdmin):
list_display=('emp_id','ename','post','salary')
admin.py
from django.contrib import admin
from .models import Student,StudentAdmin,Employee,EmployeeAdmin
# Register your models here.
admin.site.register(Student,StudentAdmin)
admin.site.register(Employee,EmployeeAdmin)
Successfully Django application to store and retrieve data from a database using Object Relational Mapping(ORM).
