• Django中ORM操作


    ORM操作:

            

             class UserInfo(models.Model):

                       username = models.CharField(max_length=32)

                       password = models.CharField(max_length=64)

                       dp = models.ForeignKey(to='DePart',to_field='id')

                       # depart = models.ForeignKey('DePart')

                      

             class DePart(models.Model):

                       title = models.CharField(max_length=16)

                      

             # ForeignKey查询

            

             # 正向跨表查询

             # 1.对象

             # q = models.UserInfo.objects.all()

             # for row in q:

             #      print(row.username,row.password,row.dp_id,row.dp.id,row.db.title)

                      

             # 2.字典

             # q = models.UserInfo.objects.values('username','password','dp_id','dp__title')

             # for row in q:

             #      print(row['username'],row['dp__title'])

                      

             # 3.元祖

             # q = models.UserInfo.objects.values_list('username','password','dp_id','dp__title')

             # for row in q:

             #      print(row[0],row[3])

            

            

             # 反向跨表查询

             # 1.对象

             # v = models.DePart.objects.all()

             # for row in v:

             #      print(row.id,row.title,row.userinfo_set.all())

                      

             # 2.字典

             # v = models.DePart.objects.values('id','title','userinfo__username','userinfo__password')

             # for row in v:

             #       print(row)

            

             # 3.元祖

             # v = models.DePart.objects.values('id','title','userinfo__username','userinfo__password')

             # for row in v:

             #      print(row)

            

            

             # 自己写第三张表

                       def U2G(models.Model):

                                ui = models.ForeignKey('UserInfo')

                                ug = models.ForeignKey('UserGroup')

                      

                       # 添加数据

                       models.U2G.objects.create(ui=1,ug=1)

                       models.U2G.objects.create(ui=1,ug=2)

                       models.U2G.objects.create(ui=2,ug=1)

                       models.U2G.objects.create(ui=2,ug=2)

                      

                       # 查询

                       q = models.U2G.objects.all()

                       for row in q:

                                print(row.ui.username)

                                print(row.ug.caption)

            

             # django 自动生成

            

                       m = models.ManyToManyField('UserInfo')

                      

                       增加:

                                obj = models.UserGroup.objects.filter(id=2).first()

                                obj.m.add(1)

                                obj.m.add(1,2,3)

                                obj.m.add(*[1,2]) #一次多加一个

                               

                       删除:

                                obj = models.UserGroup.objects.filter(id=2).first()

                                obj.m.remove(1)

                                obj.m.remove(2,3,4)

                                obj.m.remove(*[2,3,4])  

                               

                       清空:

                                obj.m.clear() # 没有参数

                               

                       更新:设置为什么, 最后就只有什么

                                obj.m.set([3,])

                               

                       查询:

                                q = obj.m.all()

                                print(q)

                               

                                q = obj.m.filter()

            

                       

    最新内容可以看我的blog: 40kuai
  • 相关阅读:
    Python 批量修改图片格式和尺寸
    c++ placement new概念
    xcopy
    STL List::sort() 解析
    程序猿的骄傲,以及骄傲背后真实的原因。
    误人子弟的网络,谈谈HTTP协议中的短轮询、长轮询、长连接和短连接
    【niubi-job——一个分布式的任务调度框架】----niubi-job这下更牛逼了!
    程序员面经:面试前到底该不该刷题以及面试前该如何准备
    送给即将毕业的同学,谈谈毕业后第一份工作和追女生的问题。
    一个最新发现,原来程序员的最终归宿在这里。
  • 原文地址:https://www.cnblogs.com/40kuai/p/6952089.html
Copyright © 2020-2023  润新知