1 增加
增加数据有两种方法。
1)save
通过创建模型类对象,执行对象的save()方法保存到数据库中。
>>> from datetime import date
>>> book = BookInfo(
btitle='西游记',
bput_date=date(1988,1,1),
bread=10,
bcomment=10
)
>>> book.save()
>>> hero = HeroInfo(
hname='孙悟空',
hgender=0,
hbook=book
)
>>> hero.save()
>>> hero2 = HeroInfo(
hname='猪八戒',
hgender=0,
hbook_id=book.id
)
>>> hero2.save()
2)create
通过模型类.objects.create()保存。
>>> HeroInfo.objects.create(
hname='沙悟净',
hgender=0,
hbook=book
)
<HeroInfo: 沙悟净>
2 查询
2.1 基本查询
get 查询单一结果,如果不存在会抛出模型类.DoesNotExist异常。
all 查询多个结果。
count 查询结果数量。
&