1.创建Model,进入models.py
输入类似下面的代码:
classPublisher(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
classAuthor(models.Model):
name =models.CharField(max_length=30)
age = models.IntegerField()
email =models.EmailField()
def __unicode__(self):
return self.name
classBook(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
上面的每个class相当于一个新的table
django会自动给每个model配置一个名为id的primary key
2.安装model
在最初的配置文件settings.py中找到
INSTALLED_APPS
加入自己的app名称,比如'books'
3.同步数据库(create table):
在shell中输入
python manage.py makemigrations
此操作建立了模型到表的映射关系
python manage.pymigrate
此操作生成表
之后你应该能看到诸如:
Creating tables ...
Creating table books_publisher
Creating table books_author
Creating table books_book_authors
Creating table books_book
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
这样的信息
4. (插入一个新的对象) insert:
from books.modelsimport Author
方法1:
p1 = Author(name='zhangsan',age=30,email='zhangsan@126.com')
p1.save()
方法2:
p1 = Author()
p1. name='zhangsan'
p1.age =30
p1.email='zhangsan@126.com'
p1.save()
方法3:
Author.objects.create(name='zhangsan',age=30,email='zhangsan@126.com')
--insert intobooks_author values('zhangsan', age=30,'zhangsan@126.com')
方法4:
Author.objects.get_or_create(name='zhangsan',age=30,email='zhangsan@126.com)
这种方法是防止重复很好的方法,但是速度要相对慢些, 返回一个元组, 第一个为Author对象, 第二个为True或False,新建时返回的是True,已经存在时返回False.
5.额外问题:为model加入字符串表现形式
在每个model里加入:
def __unicode__(self):
returnself.name
return处写上你想要的字符串
6.(查询或选择对象)select:
6.1获取一个记录对象:
Author.objects.get(name="zhangsan")
--select * from books_author where name='zhangsan'
注意:get返回的对象具有唯一性质,如果符合条件的对象有多个,则get报错!
6.2获取所有记录对象:
Author.objects.all()
-- select * from books_author
6.3获取所有切片
Author.objects.all()[0:3]
6.4获取满足某些条件的记录对象
名字完全等于zhangsan:
Author.objects.filter(name='zhangsan')
Author.objects.filter(name__exact='zhangsan')
——相当于select from books_author where name = 'zhangsan'
名字对zhangsan大小写不敏感:
Author.objects.filter(name__iexact='zhangsan')
——相当于select from books_author where upper(name)='ZHANGSAN'
名字包含zhang:
Author.objects.filter(name__contains="zhang")
——相当于select from ... where name LIKE '%zhang%';
查询条件使用正则:
区分大小写的正则:
Author.objects.filter(name__regex='^zhang')
不区分大小写的正则:
Author.objects.filter(name__iregex='^zhang')
以上方法,返回的都是一个list(其实是query set)
6.5获取排除某些条件的记录对象
排除name等于zhangsan的记录:
Author.objects.exclude(name="zhangsan")
排除name中包含zhang的记录:
Author.objects.exclude(name__contains="zhang")
以上filter和exclude也可以组合使用:
获取姓zhang但是不叫zhangsan的:
Author.objects.filter(name__iregex='^zhang').exclude(name__contains='san')
7.(对结果排序)order:
Author.objects.order_by("name")
——相当于select from ... ORDER BY name;
多项排序:
Author.objects.order_by("age","name")
逆向排序:-name
Author.objects.order_by("-name")
连锁查询:
Author.objects.filter(age=30).order_by("-name")
8.(更新对象)update:
1)使用save()方法:
p = Author.objects.get(name='Zhangsan')
p.name = 'zhangsan'
p.save()
——相当于
UPDATE books_Author SET
name = 'zhangsan',
age = 30,
email = 'zhangsan@126.com'
WHERE name='Zhangsan';
所有的列都被更新了!效率低!
2)用update()方法才比较好:
Author.objects.filter(name='Zhangsan').update(name='zhangsan')
——相当于
UPDATEbooks_Author
SETname= 'zhangsan'
WHEREname='Zhangsan';
可以对多行同时进行更新:
Author.objects.all().update(country='USA')
update()方法会返回一个整型数值,表示受影响的记录条数
9.(删除对象)delete:
调用该对象的delete()方法即可:
p = Author.objects.get(name="O'Reilly")
p.delete()
一旦使用all()方法,所有数据将会被删除:
Author.objects.all().delete()
10.调用原始sql
a = Author.objects.raw("select * from bolls_author where age >= 30 and age <= 35")
* a返回时list for 对a 遍历.
for i in a :
print (i)