Django 紧紧地遵循这种 MVC 模式,可以称得上是一种 MVC 框架。以下是 Django 中 M、V 和 C 各自的含义:
由于 C 由框架自行处理,而 Django 里更关注的是模型(Model)、模板(Template)和视图(Views),Django 也被称为 MTV 框架 。在 MTV 开发模式中:
models.py就是Django里面的建表所需的数据模型的描述: 命令: python manage.py init 初始化数据库 python manage.py sqlall [appname] 查看app的CREATE TABLE的语句,包括原始数据,创建索引等. python manage.py sqlreset [appname] 修改models,不保留以前数据,进行重置数据库,更新表。 python manage.py validate 用来排错 python manage.py syncdb 用来建表 python manage.py sql databasename 用来查看已创建数据库表结构 数据库建表过程: 1.创建应用程序 python manage.py startapp appname 2.用python代码写models.py,创建数据模型 from django.db import models class Publisher(models.Model): name = models.CharField(maxlength=30) address = models.CharField(maxlength=50) city = models.CharField(maxlength=60) state_province = models.CharField(maxlength=30) country = models.CharField(maxlength=50) website = models.URLField()3.模型的安装 编辑settings.py 添加: INSTALLED_APPS=( 'mysite.appname', } 4.创建数据库表: python manage.py validate (验证模型有效性) python manage.py sqlall appname (生成CREATE TABLE语句) python manage.py syncdb (执行上一步产生的SQL语句,简表。不执行上一句也可以建,也可以把上一句执行产生的SQL语句放入SQL客户端去执行,syncdb 仅仅创建数据库中不存在的表,而不会同步模型的修改或者删除到数据库) 5.插入和更新数据 插入数据: 定义对象 p = Publisher(.....) //将新的内容放到Publisher里面赋给对象p p.save() //完成插入 上面相当于SQL语言: INSERT INTO book_publisher (name, address, city, state_province, country, website) VALUES ('Apress', '2855 Telegraph Ave.', 'Berkeley', 'CA', 'U.S.A.', 'http://www.apress.com/');更新数据: p.name = 'NewName' //修改对象p的名字 p.save() //完成存储 上面相当于SQL语言: UPDATE book_publisher SET name = 'NewName', address = '2855 Telegraph Ave.', city = 'Berkeley', state_province = 'CA', country = 'U.S.A.', website = 'http://www.apress.com' WHERE id = 52;选择对象: Publisher.objects.all() 相当于SQL: SELECT id, name, address, city, state_province, country, website FROM book_publisher;数据过滤: Publisher.objects.filter(name="NewName") 相当于SQL语句: SELECT id, name, address, city, state_province, country, website FROM book_publisher WHERE name = 'NewName'; 缩小范围: Publisher.objects.filter(country="U.S.A.", state_province="CA") 相当于SQL语句: SELECT id, name, address, city, state_province, country, website FROM book_publisher WHERE country = 'U.S.A.' AND state_province = 'CA'; 模糊匹配: Publisher.objects.filter(name__contains="press") 在 name 和 contains 之间有双下划线。象Python自己一样,Django也使用 双下划线来做一些小魔法,这个 __contains 部分会被Django转换成 LIKE SQL语句: SELECT id, name, address, city, state_province, country, website FROM book_publisher WHERE name LIKE '%press%';获取单个对象: Publisher.objects.get(name="Apress Publishing")数据排序: Publisher.objects.order_by("name") 相当于SQL语句: SELECT id, name, address, city, state_province, country, website FROM book_publisher ORDER BY name; 可以支持多个字段排序: Publisher.objects.order_by("state_provice", "address") 也可以逆向排序: Publisher.objects.order_by("-name") 添加‘-’同时排序和过滤: Publisher.objects.filter(country="U.S.A.").order_by("-name") 相当于SQL语句: SELECT id, name, address, city, state_province, country, website FROM book_publisher WHERE country = 'U.S.A' ORDER BY name DESC; 限制返回数据: Publisher.objects.all()[Number] //Number可以换成想要显示的位置下标 相当于SQL语句: SELECT id, name, address, city, state_province, country, website FROM book_publisher ORDER BY name LIMIT 1; 删除对象:p = Publisher.objects.get(name="NewName") p.delete() 修改数据库表结构: 添加字段: 首先,在开发环境中执行下面的步骤(也就是说,不是在发布服务器上):
ALTER TABLE books_book DROP COLUMN test;删除表:DROP TABLE books_book |