1.创建一个django app:
python manage.py startapp books
2.validate 命令检查你的模型的语法和逻辑是否正确。一旦你觉得你的模型可能有问题,运行 python manage.py validate 。 它可以帮助你捕获一些常见的模型定义错误。
3.django1.7后,数据库迁移只有三个命令
(1)migrate,用来迁移数据库。
用法:
python manage.py migrate app_name
(2)makemigrations,用来检测数据库变更和生成数据库迁移文件。
用法:
python manage.py makemigratioins app_name
(3)sqlmigrate,用来把数据库迁移文件转换成数据库语言查看(displays the SQL statements for a migratioin.)
用法:sqlmigrate app_name migration_name,比如makemigrations生成了0001_initial.py,就用sqlmigrate app 0001_intial,这里0001_initial就是migration_name参数。
python manage.py sqlmigrate app_name 0001_initial
一般如果某次migration使用sqlmigrate没有提示错误,那么在migrate时就能成功。
4.终端上输入 python manage.py 可以看到详细的命令列表,在忘记命令子名称的时候特别有用。
5.Django为模型提供了高级的Python API:
(1)创建并保存对象实例
>>> from books.models import Publisher >>> p1 = Publisher(name='Apress', address='2855 Telegraph Avenue', ... city='Berkeley', state_province='CA', country='U.S.A.', ... website='http://www.apress.com/') >>> p1.save() >>> p2 = Publisher(name="O'Reilly", address='10 Fawcett St.', ... city='Cambridge', state_province='MA', country='U.S.A.', ... website='http://www.oreilly.com/') >>> p2.save() >>> publisher_list = Publisher.objects.all() >>> publisher_list [<Publisher: Publisher object>, <Publisher: Publisher object>]
当使用Django modle API创建对象时Django并未将对象保存至数据库内,除非调用`` save()`` 方法。
如果需要一步完成对象的创建与存储至数据库,就使用`` objects.create()`` 方法。
>>> p1 = Publisher.objects.create(name='Apress', ... address='2855 Telegraph Avenue', ... city='Berkeley', state_province='CA', country='U.S.A.', ... website='http://www.apress.com/') >>> p2 = Publisher.objects.create(name="O'Reilly", ... address='10 Fawcett St.', city='Cambridge', ... state_province='MA', country='U.S.A.', ... website='http://www.oreilly.com/') >>> publisher_list = Publisher.objects.all() >>> publisher_list
(2)为了把不同对象实例区分开,可以用__unicode()__方法给他们起名字。
from django.db import models 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
这样的话:
>>> from books.models import Publisher >>> publisher_list = Publisher.objects.all() >>> publisher_list [<Publisher: Apress>, <Publisher: O'Reilly>]
(3)对某一个字段修改后再save,所有字段都会更新。所以要更改某一指定的列,我们可以调用结果集(QuerySet)对象的update()方法,更好。
>>> Publisher.objects.filter(id=52).update(name='Apress Publishing') >>> Publisher.objects.all().update(country='USA')
(4)数据过滤
>>> Publisher.objects.filter(country="U.S.A.", state_province="CA")#多个参数相当于SQL里的AND语句 [<Publisher: Apress>]
name__contains相当与SQL的LIKE语句
>>> Publisher.objects.filter(name__contains="press") [<Publisher: Apress>] #相当于以下 SELECT id, name, address, city, state_province, country, website FROM books_publisher WHERE name LIKE '%press%';
(5)上面的例子中filter()函数返回一个记录集,这个记录集是一个列表。 相对列表来说,有些时候我们更需要获取单个的对象,get()方法就是在此时使用的:
>>> Publisher.objects.get(name="Apress") <Publisher: Apress>
如果结果是多个对象或者没有对象,会抛出异常。
(6)数据排序
>>> Publisher.objects.order_by("address") [<Publisher: O'Reilly>, <Publisher: Apress>] >>> Publisher.objects.order_by("state_province", "address")#第一个参数相同的话,按后面的参数排 [<Publisher: Apress>, <Publisher: O'Reilly>] >>> Publisher.objects.order_by("-name")#逆向排 [<Publisher: O'Reilly>, <Publisher: Apress>]
也可以规定默认的顺序:
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 Meta: ordering = ['name']
(7)可以写成链式查询:
>>> Publisher.objects.filter(country="U.S.A.").order_by("-name") [<Publisher: O'Reilly>, <Publisher: Apress>]
#相当于以下:
SELECT id, name, address, city, state_province, country, website FROM books_publisher WHERE country = 'U.S.A' ORDER BY name DESC;
(8)限制返回的数据:
>>> Publisher.objects.order_by('name')[0:2] >>> Publisher.objects.order_by('name')[0]
注意这里不支持python的负索引。
(9)删除:
>>> Publisher.objects.all().delete() >>> Publisher.objects.filter(country='USA').delete()
6.