Model-View-Controller (MVC)¶
- In MVC, the
model
represents the information (the data) of the application and the business rules used to manipulate the data - The
view
corresponds to elements of the user interface such as text, checkbox items, and so forth; - The
controller
manages details involving the communication to the model of user actions.
Model¶
- It defines the schema of the database, including tables, fields, and relationships.
Models
are typically defined inmodels.py
files within Django apps.- We call the Data bit - the “Model” or Data Model
View¶
- The HTML, CSS, etc. which makes up the look and feel of the application.
Views
in Django are typically functions or classes defined inviews.py
files within Django apps.- We call the “making the next HTML” bit the “View” or “Presentation Layer”
Template¶
Templates
in Django are responsible for the user interface.- They define the structure and presentation of the data to be displayed to the user. Django uses its built-in templating engine to render dynamic content based on data provided by views.
Controller¶
- The code that does the thinking and decision making
- We call the handling of input and the general orchestration of it all the “Controller”
Object Relational Mapping (ORM)¶
Object-Relational Mapping (ORM)
in Django allows you to interact with your database using high-level object-oriented constructs instead of directly writing SQL queries.
1.Define a model (table)¶
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=50)
publication_date = models.DateField()
2.Create Migration and Apply to Database¶
Django
provides a command-line interface to generate database migrations based on your model definitions.
cmd
python manage.py makemigrations
python manage.py migrate
- This creates the necessary database tables based on your models.
3.a Input to table / database¶
Now, you can use the Book
model in your Python code.
- For example, to create a new book:
from myapp.models import Book
from datetime import date
# Create a new book
book = Book.objects.create(
title='Python Programming',
author='Guido van Rossum',
publication_date=date(1995, 1, 1)
)
3.b Query the database¶
You can query the database using Django's ORM
to retrieve objects.
- For example, to retrieve all books written by a specific author:
# Query all books by a specific author
books = Book.objects.filter(author='Guido van Rossum')
for book in books:
print(book.title)
3.c CREATE, READ, UPDATE and DELETE (CRUD) in the ORM¶
# Create a new user and save it to the database
u = User(name = "Sally", email = "a2@umich.edu")
u.save()
# Retrieve all users from the database and display their values
User.objects.values()
# Filter users by email and display their values
User.objects.filter(email = "ted@umich.edu").values()
User.objects.filter(email = "ted@umich.edu").delete()
User.objects.values()
# Update the name of the user(s) with a specific email
User.objects.filter(email = "csev@umich.edu").update(name = 'Charles')
User.objects.values()
# Retrieve all users from the database, order them by email, and display their values
User.objects.values().order_by("email")
# Retrieve all users from the database, order them by name in descending order, and display their values
User.objects.values().order_by("-name")
Migrations: From model to Database¶
The
make migrations
command reads all themodels.py
files in all the applications, end creates / evolves the migration filesGuided by the applications listed in
settings.py