通过模型类操作数据表
一对多类
class HeroInfo(models.Model):
"""英雄人物类"""
hname = models.CharField(max_length=20)
# 性别 False 指男性
hgender = models.BooleanField(default=False)
# 备注
hcomment = models.CharField(max_length=128)
# 关系属性 建立图书类与英雄人物类 一对多关系
hbook = models.ForeignKey('BookInfo',on_delete=models.CASCADE)
shell
from booktest.models import BookInfo,HeroInfo
from datetime import date
b = BookInfo()
b.btitle = '天龙八部'
b.bpub_date = date(2020,1,28)
b.save()
h = HeroInfo()
h.hame = '段誉'
h.hgender = False
h.hcomment = '六脉神剑'
h.hbook = b
h.save()
h2 = HeroInfo()
h2.hname = '乔峰'
h2.hcomment = '降龙十八掌'
h2.hbook = b
h2.save()
h3 = HeroInfo.objects.get(id=2)
h3.hname
h3.hbook
h3.hbook_id
h3.hbook.btitle
b.heroinfo_set.all()
BookInfo.objects.all()
HeroInfo.objects.all()
网友评论