Lesson 12 - Data CRUD Operations
LEARN
Week 5 - Data
- Lesson 12 - Data CRUD Operations
- Lesson 13 - View Inheritance
- Lesson 14 - Manipulating Data
- Project 5 - Admin Views
- Django for Beginners - Chapter 5 - Blog App
Visit the Django website
- Django Website
- Django Start
- Select the "Object-relational Mapper" expansion link
Applications = Data + Views
- Static Websites are views without data
- Data is saved state on the server
ORM - Object Relational Mapper
- Create Python Classes
- Automatically create Database Tables
Database
- Rows and Columns
- Items and Attributes
Two Views of Data
- Objects in Memory
- Database Rows
Superhero Data Model
- Superhero
- Name
- Description
- Image
Data Migration
- Examine Python Data Models
- Create scripts to migrate the database tables
CRUD Operations
- CREATE - make a new record
- READ - get one or more records (List, Detail)
- UPDATE - modify an existing record
- DELETE - remove a record
Operations on Every Model
- Create Superhero
- List Superhero
- Details Superhero
- Update Superhero
- Delete Superhero
Change in Demos
- Weeks 1-4 - monkey see; monkey do
- Weeks 5-14 - learn and debug
Book Builder App
- Simple enough to teach principles
- Complex enough to demonstrate key ideas
- Every week we will build features
- Projects 1-14 will build Superhero News Service
Django Admin View
- Build a UI to manipulate data
- Free and easy
- Create new table in five minutes
BUILD
Short-cuts
- Sensei
- Github repo
- Web app
- Python Anywhere Web config
- Python Anywhere Console
Development Workflow
- Pull
- Change
- Test
- Push
- Deploy
Start Book Builder project
$ cd week5/BookBuilder
$ django-admin startproject config .
$ python manage.py startapp book
Settings
config/settings.py
# Enable the templates for the 'templates' directory
TEMPLATES = [
{
...
'DIRS': [BASE_DIR / 'templates'],
...
},
]
# Enable the static media server (Images, CSS, Javascript)
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
# Place to collect static files
STATIC_ROOT = BASE_DIR / "static_assets"
# Enable Python Anywhere
ALLOWED_HOSTS = ['markseaman.pythonanywhere.com', '127.0.0.1', 'localhost']
# Enable data for Hero app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'book',
]
Add Data Model
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
$ python manage.py makemigrations
$ python manage.py migrate
Enable Admin Views on Book
book/admin.py
from django.contrib import admin
from .models import Book
admin.site.register(Book)
Test Admin Book
$ python manage.py createsuperuser
$ python manage.py runserver
$ browse to 127.0.0.1:8000/admin/
$ git add . && git commit -m "Book Data Model"
Python CRUD
Terminal Window
$ python manage.py shell
Use the Book Model
from book.models import Book
List Books
Book.objects.all()
Book.objects.filter(author='Mark Seaman')
Get specific book
b = Book.objects.get(title="A Seaman's Journey")
b.author
b.name
b.pk
Create book record
Book.objects.create(title="A Seaman's Journey", author="Mark Seaman")
Update book record
b = Book.objects.get(title="A Seaman's Journey")
b.title = "A Seaman's Life"
b.save()
Delete a record
Book.objects.get(title="A Seaman's Journey").delete()
Practice
- Start a new project "week5/Superhero"
- Implement the Hero model and admin views
- Run the Django shell
- Experiment with creating, listing, modifying objects
CRUD Code
- CREATE - Superhero.objects.create()
- READ - Superhero.objects.get()
- UPDATE - hero.save()
- DELETE - hero.delete()
Data Model
- Superhero (name, description='None', image='None')
hero/models.py
class Superhero(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
image = models.CharField(max_length=200)
CREATE
Superhero.objects.create(name='Black Widow', description='Natalia Romanova', image='None')
READ
w = Superhero.objects.get(name='Black Widow')
print(w.description)
for s in Superhero.objects.all():
print(w.description, w.name)
UPDATE
w = Superhero.objects.get(name='Black Widow')
w.description='Natasha Romanoff'
w.save()
DELETE
Superhero.objects.get(name='Black Widow').delete()