The Django Book - Chapter 5: Models

29
The Django Book— Models Sharre Chen http://django-book.readthedocs.org/en/latest/index.html

Transcript of The Django Book - Chapter 5: Models

The Django Book—Models

Sharre Chen

http://django-book.readthedocs.org/en/latest/index.html

The “Dumb” Way to Do Database Queries in Views

Django’s database API

MVC

M(Model): data access logic, is handled by Django’s database layer

V(View): business logic, is handled by views and templates.

C(Controller): presentation logic, is handled by the framework itself by following your URLconf and calling the appropriate Python function.

MTV

M(Model): data access layer.

T(Template): the presentation layer.

V(View): business logic layer.

Configuring the Database

• In settings.py:

Example for MySQL: DATABASE_ENGINE = 'mysql'DATABASE_NAME = 'mydb'DATABASE_USER= 'user'DATABASE_PASSWORD = 'password'DATABASE_HOST = ‘/var/run/mysql'!Try if setting is correct:$ python manage.py shell>>> from django.db import connection>>> cursor = connection.cursor()

What’s the difference between a project and an app?

• A project is an instance of a certain set of Django apps, plus the configuration for those apps.

• An app is a portable set of Django functionality, usually including models and views, that lives together in a single Python package.

★ if you’re using Django’s database layer (models), you must create a Django app.

Create an app

create a books app:

$ python manage.py startapp booksfile structure will look like:

Data access with Django

Installing the Model

Edit setting.py file again:

!

!

!

!

Check models’ syntax

checks whether your models’ syntax and logic are correct:

$ python manage.py validate

check create table sqlIf your models are valid, run the following command for Django to generate CREATE TABLE statements for your models in the books app (before migration):

$ python manage.py sqlall books

commit the SQL to database

$ python manage.py syncdb

!

!

★ Note that syncdb does not sync changes in models or deletions of models.

Basic data access

Try it out:$ python manage.py shell

Basic data access

If you want to create an object and save it to the database in a single step, use the objects.create() method.

Adding Model String Representations

it difficult to tell the Publisher objects apart:

Inserting and Updating Data

update name:

will result in:

Selecting Objects

if you type command:

This roughly translates to this SQL:

!

✦ Zen of Python: “Explicit is better than implicit.”

Filtering Data

filter data:

This roughly translates to this SQL:

!

Filtering Data: LIKE

filter data:

!

here, the __contains part gets translated by Django into a SQL LIKE statement:

!

Retrieving Single Objects

get single data:

will result in:

!

!

you may write:

Ordering Data

get ordered data:

SQL will be:

!

To order by multiple fields:

!

specify reverse ordering:

Ordering Data

Specify a default ordering in model:

Chaining Lookups

get filtered and ordered data:

!

As SQL:

Slicing Data

get first ordered data:

!

As SQL:

Slicing Data

but can not do:

!

!

you can do in another way:

Updating Multiple Objects in One Statement

update a filed:

!

!

update will return success updated object number:

Deleting Objects

delete a data:

!

!

delete all matched data:

Q&A

Thank You ~ :)