Download - Django: Advanced Models
![Page 1: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/1.jpg)
Chapter 10Advanced Model
Anne LaiDjango Girls meetup
2015/10/8
Django Book
![Page 2: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/2.jpg)
• Related Objects
• Making Changes to a Database Schema
• Manager
• Model Methods
• Executing Raw SQL Queries
Outline
![Page 3: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/3.jpg)
Recall of Chapter 5
• Define models • Use the database API to create,
retrieve, update and delete records
![Page 4: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/4.jpg)
Related Objects
![Page 5: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/5.jpg)
Related Objects
• ForeignKey
• ManyToManyField
![Page 6: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/6.jpg)
Related Objects
![Page 7: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/7.jpg)
Related Objects
✐return QuerySet values instead of model instances
![Page 8: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/8.jpg)
Related Objects
✐ book_set = lowercase(Book) + “_set”QuerySet
![Page 9: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/9.jpg)
Making Changes to a Database Schema
![Page 10: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/11.jpg)
Making Changes to a Database Schema
• Adding Fields
• Removing (normal/ Many-to-Many) Fields
• Removing Models
![Page 12: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/17.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/18.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/20.jpg)
Manager
![Page 21: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/21.jpg)
Manager
• Adding Extra Manager Methods
• Modifying Initial Manager QuerySets
• Multiple Managers
✐ Managers = database queries
![Page 22: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/22.jpg)
Adding Extra Manager Methods
![Page 23: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/23.jpg)
Adding Extra Manager Methods
✐ manager itself (objects)
✐ extends django.db.models.Manager
✐ replacing the “default” manager
![Page 24: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/24.jpg)
Modifying Initial Manager QuerySets
QuerySet
![Page 25: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/25.jpg)
Multiple Managers
✐ Django interprets the first Manager defined in a class as the “default” Manager
![Page 26: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/26.jpg)
Model Methods
![Page 27: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/27.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/28.jpg)
• 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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/29.jpg)
Executing Raw SQL Queries
![Page 30: Django: Advanced Models](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/30.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062503/58f318f11a28ab487b8b45df/html5/thumbnails/31.jpg)
Thank you