Django: Advanced Models

31
Chapter 10 Advanced Model Anne Lai Django Girls meetup 2015/10/8 Django Book

Transcript of Django: Advanced Models

Page 1: Django: Advanced Models

Chapter 10Advanced Model

Anne LaiDjango Girls meetup

2015/10/8

Django Book

Page 2: Django: Advanced Models

• Related Objects

• Making Changes to a Database Schema

• Manager

• Model Methods

• Executing Raw SQL Queries

Outline

Page 3: Django: Advanced Models

Recall of Chapter 5

• Define models • Use the database API to create,

retrieve, update and delete records

Page 4: Django: Advanced Models

Related Objects

Page 5: Django: Advanced Models

Related Objects

• ForeignKey

• ManyToManyField

Page 6: Django: Advanced Models

Related Objects

Page 7: Django: Advanced Models

Related Objects

✐return QuerySet values instead of model instances

Page 8: Django: Advanced Models

Related Objects

✐ book_set = lowercase(Book) + “_set”QuerySet

Page 9: Django: Advanced Models

Making Changes to a Database Schema

Page 10: Django: Advanced Models

Making Changes to a Database Schema

• syncdb

- sync model to the database

- merely creates tables that don’t yet exist in database

- does not sync changes in models or perform deletions of models

Page 11: Django: Advanced Models

Making Changes to a Database Schema

• Adding Fields

• Removing (normal/ Many-to-Many) Fields

• Removing Models

Page 12: Django: Advanced Models

Adding Fields

Development Environment

1. Add the field to your model

✐a database column will contain

NULL values when first created.

Page 13: Django: Advanced Models

Adding Fields

2. Run manage.py sqlall [app] to see the new CREATE TABLE statement for the

model

Development Environment

✐column definition for the new field

Page 14: Django: Advanced Models

Adding Fields

3. Start database’s interactive shell

(e.g., psql or mysql, or you can use manage.py dbshell)

Execute an ALTER TABLE statement that adds your new column

Development Environment

NULL column

NOT NULL column

Page 15: Django: Advanced Models

Adding Fields

4. Verify with Python interactive shell manage.py shell

Development Environment

✐ If a model contains a field that has not yet

been created in the database table, it will cause

an error happens at code execution time.

Page 16: Django: Advanced Models

Adding Fields

Development Environment

1. Add the field to your model.

2. Run manage.py sqlall [app] to see the new CREATE TABLE statement for the

model. Note the column definition for the new field.

3. Start database’s interactive shell (e.g., psql or mysql, or you can use

manage.py dbshell). Execute an ALTER TABLE statement that adds your new

column.

4. Verify with Python interactive shell manage.py shell by importing the

model and selecting from the table (e.g., MyModel.objects.all()[:5]).

Page 17: Django: Advanced Models

Adding Fields

Production Server

1. Start database’s interactive shell (e.g., psql or mysql, or you can use

manage.py dbshell)

2. Execute the ALTER TABLE statement

3. Add the field to your model. If you’re using source-code revision control

and you checked in your change in development environment step 1,

now is the time to update the code (e.g., svn update, with Subversion) on

the production server

4. Restart the Web server

Page 18: Django: Advanced Models

Removing Fields

1. Remove the field from model and restart the Web server.

2. Remove the column from your database, using a command like this:

✐ Order sensitive

1. Remove the ManyToManyField from model and restart the Web server.

2. Remove the many-to-many table from your database,

using a command like this:

Normal Field

Many-to-Many Field

Page 19: Django: Advanced Models

Removing Models

1. Remove the model from your models.py file and restart the Web server.

2. Remove the table from your database, using a command like this:

✐ Order sensitive

Page 20: Django: Advanced Models

Manager

Page 21: Django: Advanced Models

Manager

• Adding Extra Manager Methods

• Modifying Initial Manager QuerySets

• Multiple Managers

✐ Managers = database queries

Page 22: Django: Advanced Models

Adding Extra Manager Methods

Page 23: Django: Advanced Models

Adding Extra Manager Methods

✐ manager itself (objects)

✐ extends django.db.models.Manager

✐ replacing the “default” manager

Page 24: Django: Advanced Models

Modifying Initial Manager QuerySets

QuerySet

Page 25: Django: Advanced Models

Multiple Managers

✐ Django interprets the first Manager defined in a class as the “default” Manager

Page 26: Django: Advanced Models

Model Methods

Page 27: Django: Advanced Models

Model Methods

http://www.python.org/download/releases/2.2/descrintro/#property

✐property(fget=None, fset=None, fdel=None, doc=None)

Page 28: Django: Advanced Models

• Model Method:

- “row-level” functionality to objects

- Act on a particular model instance

• Managers:

- “table-wide” or “table-level”

Comparison

Page 29: Django: Advanced Models

Executing Raw SQL Queries

Page 30: Django: Advanced Models

Executing Raw SQL Queries ✐ current db connection

✐use placeholders rather than adding parameters directly within the SQL

standard Python “DB-API”: https://www.python.org/dev/peps/pep-0249/

Page 31: Django: Advanced Models

Thank you