💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
在booktest中models.py中最加个类 ~~~ # 多类 class HeroInfo(models.Model): '''英雄人物模型类''' hname = models.CharField(max_length=20) # 英雄名称 # 性别,BooleanField说明是bool类型,default指定默认值,False代表男 hgender = models.BooleanField(default=False) # 备注 hcomment = models.CharField(max_length=128) # 关系属性 hbook,建立图书类和英雄人物类之间的一对多关系 hbook = models.ForeignKey('BookInfo', on_delete=models.CASCADE, ) ~~~ 生成迁移文件 ~~~ python manage.py makemigrations ~~~ 生成表 ~~~ python manage.py migrate ~~~ ~~~ >>> from booktest.models import BookInfo,HeroInfo >>> b = BookInfo() >>> b.btitile = '天龙八部' >>> from datetime import date >>> b.bpub_date = date(1990,1,1) >>> b.save() >>> h = HeroInfo() >>> h.hname = '段誉' >>> 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.hgender False >>> h3.hcomment '降龙十八掌' ~~~ ~~~ >>> h3.hbook <BookInfo: BookInfo object (2)> ~~~ 查询所有 ~~~ >>> b.heroinfo_set.all() <QuerySet [<HeroInfo: HeroInfo object (1)>, <HeroInfo: HeroInfo object (2)>]> ~~~ 通过bookinfo类对象查询关系heroinfo信息 ![](https://box.kancloud.cn/683b861e7380562d8fc8c900e79cdf29_1762x486.png) 查询所有 ~~~ HeroInfo.objects.all() ~~~ ~~~ BookInfo.objects.all() ~~~