单表操作
增加数据
auther_obj = {"auther_name":"崔皓然","auther_age":1} models.auther.objects.create( **auther_obj )
models.auther.objects.create( auther_name="周雍博", auther_age=4 )
obj = models.auther( auther_name="崔洪艳", auther_age=29 ) obj.save()
obj = models.auther( auther_name = "王仙枝", auther_age = "50" ) obj.save()
删除数据
models.auther.objects.filter(auther_name="崔洪艳")[0].delete()
一对多操作
增加数据
a_obj = models.auther.objects.get(id=1) models.book.objects.create( book_name = "python课程1", book_price = 12, book_auther = a_obj )
a_obj = models.auther.objects.get(id=5) models.book.objects.create( book_name = "java课程1", book_price = 13, book_auther_id = a_obj.id )
a_obj = models.auther.objects.get(id=2) book_dict = { "book_name":"python课程2", "book_price":14, "book_auther_id":a_obj.id } obj = models.book.objects.create( **book_dict )
a_obj = models.auther.objects.get(id=4) book_dict = { "book_name":"java课程2", "book_price":15, "book_auther":a_obj } models.book.objects.create( **book_dict )
一对多正向查询
# 查询id为2的书的所有作者的信息 obj = models.book.objects.get(id=2) print(obj.book_auther.auther_name) print(obj.book_auther.auther_age) obj = models.book.objects.filter(id=2).values("book_name","book_price","book_auther__id","book_auther__auther_name","book_auther__auther_age") print(obj)
# 查询id为2的作者的出版的书 obj = models.book.objects.filter(book_auther_id=2).values("book_name","book_price","book_auther__id","book_auther__auther_name","book_auther__auther_age") print(obj) obj = models.book.objects.get(book_auther_id=2) print(obj.book_auther.auther_age) print(obj.book_auther.auther_name) print(obj.book_name)
一对多反向查询
# 查看出版社出版的书 obj = models.auther.objects.filter(id=2).values("auther_name","auther_age","book__book_name") print(obj) obj = models.auther.objects.get(id=2).book_set.values("book_name","book_price","book_auther__id","book_auther__auther_name","book_auther__auther_age") print(obj)
多对多操作
增加数据
b1 = models.book.objects.get(id=1) b2 = models.book.objects.get(id=2) b4 = models.book.objects.get(id=4) b5 = models.book.objects.get(id=5) models.publish.objects.get(id=1).publish_book.add( b1,b2 )
book_list = [b1,b2] models.publish.objects.get(id=2).publish_book.add( *book_list )
更新数据
book_list = [b1,b2,b4,b5] models.publish.objects.get(id=1).publish_book.set(book_list)
移除数据
models.publish.objects.get(id=1).publish_book.remove(b1) book_list = [b2, ] models.publish.objects.get(id=1).publish_book.remove(*book_list)
正向查询
obj = models.publish.objects.get(id=1).publish_book.values("book_name","publish__publish_name") print(obj) obj = models.publish.objects.filter(id=1).values("publish_name","publish_book__book_name") print(obj)
反向添加数据
p1 = models.publish.objects.get(id=1) p2 = models.publish.objects.get(id=2) p3 = models.publish.objects.get(id=3) b1.publish_set.add(p1,p2,p3)
反向删除数据
b1.publish_set.remove(p1)
反向更新数据,把remove替换为set就可以了
反向查询
obj = models.book.objects.get(id=1).publish_set.values("publish_name","publish_book__book_name")
model中多表关系和参数
多对多
一对一
model操作
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]