上一小节总结了下数据库的连接配置,实际上由于安装python的版本,上一节配置失败了。
这一节就总结下模型的使用。由于练习是在白天进行的,所以晚上我只记录下过程即可。大部分无截图。
1 在D:\mysite\mysite\books\models.py文件中编辑添加如下内容:
from django.db import models
# Create your models here.
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
def __unicode__(self):
return self.name
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
def __unicode__(self):
return self.first_name
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __unicode__(self):
return self.title
INSTALLED_APPS = (
#'django.contrib.admin',
#'django.contrib.auth',
#'django.contrib.contenttypes',
#'django.contrib.sessions',
#'django.contrib.messages',
#'django.contrib.staticfiles',
'mysite.books',
)
MIDDLEWARE_CLASSES = (
#'django.contrib.sessions.middleware.SessionMiddleware',
#'django.middleware.common.CommonMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware',
#'django.contrib.auth.middleware.AuthenticationMiddleware',
#'django.contrib.messages.middleware.MessageMiddleware',
#'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
D:\mysite>python manage.py validate
0 errors found
4 再执行python manage.py sqlall books命令,查看生成的sql 。可以看到如下提示:注意,这里仅仅是打印出来。
D:\mysite>python manage.py sqlall books
BEGIN;
CREATE TABLE "books_publisher" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(30) NOT NULL,
"address" varchar(50) NOT NULL,
"city" varchar(60) NOT NULL,
"state_province" varchar(30) NOT NULL,
"country" varchar(50) NOT NULL,
"website" varchar(200) NOT NULL
)
;
CREATE TABLE "books_author" (
"id" integer NOT NULL PRIMARY KEY,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(40) NOT NULL,
"email" varchar(75) NOT NULL
)
;
CREATE TABLE "books_book_authors" (
"id" integer NOT NULL PRIMARY KEY,
"book_id" integer NOT NULL,
"author_id" integer NOT NULL REFERENCES "books_author" ("id"),
UNIQUE ("book_id", "author_id")
)
;
CREATE TABLE "books_book" (
"id" integer NOT NULL PRIMARY KEY,
"title" varchar(100) NOT NULL,
"publisher_id" integer NOT NULL REFERENCES "books_publisher" ("id"),
"publication_date" date NOT NULL
)
;
CREATE INDEX "books_book_authors_36c249d7" ON "books_book_authors" ("book_id");
CREATE INDEX "books_book_authors_e969df21" ON "books_book_authors" ("author_id")
;
CREATE INDEX "books_book_81b79144" ON "books_book" ("publisher_id");
COMMIT;
D:\mysite>
6 按照如下的方式执行,数据已经创建。可以查看到该表的数据。
D:\mysite>python manage.py shell
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from mysite.books.models import Publisher
>>> Publisher.objects.all()
[, ]
>>>