zoukankan      html  css  js  c++  java
  • 八月22日,django知识点总结:

    八月22日,知识点总结:
    
    
    python manage.py makemigrations
    python manage.py migrate

      unique=true是指这个字段的值在这张表里不能重复,所有记录值都要唯一,就像主键那样搜索nullable=false是这个字   段在保存时必需有值,不能还是null值就调用save去保存入库

    一、增删改查
    
    	1、添加表内容(三种方式)
    		1.1 obj = models.UserType(列名=‘内容’)
    			obj.save()
    		1.2 models.UserType.objects.create(列名=‘内容’)
    
    		1.3user_dict={'列名':‘内容’,'列名','内容’}
    			models.UserType.objects.create(**user_dict)
    
    
    	2、查询数据
    		2.1 request.POST.get('usernaem')
    			#获取单条数据,不存在则报错(不建议)
    
    		2.2 ret =models.UserType.objects.all()
    			#获取全部
    			print(type(ret),ret.query)
    			for item in ret:
    				#根据循环可以输出表内容
    				print(item,item.nid,item.caption)
    				
    			#ret.query 返回一个原生SQL
    			#ret,得到一个特殊的QuerySet对象
    			
    		2.3 ret =models.UserType.objects.all().values('nid')
    			print(ret)
    			
    			#values('xxx') #返回一个列表中包含字典
    		
    		2.4 ret=models.UserType.objects.all().values_list('nid')
    			print('ret')
    			
    			#返回一个列表中加元组
    			
    			
    		2.5 model.UserType.objects.filter(name='name')
    			# 获取指定条件的数据
    			
    
    	3、更改数据库内容
    		3.1 models.UserInfo.objects.filter(user='alex').update(email='123@qq.com')
    			#将指定的条件更新到数据库,可以使用字典**kwargs
    		
    		3.2 obj = models.UserInfo.objects.get(user='alex')
    			obj = email='!!!!@!!!!!'
    			obj.save()
    			#修改单条数据
    			
    
    		
    	4、删除数据
    		4.1models.UserInfo.objects.filter(name='alex').delete()
    			按条件删除
    	
    	
    	
    二、双下划线操作
    	1.连表操作
    		1.1 ret =models.UserInfo.objects.all().values('user','user_type__caption')
    			##user_type__caption  __双下划线在这是执行连表操作,输出另外表内容
    		
    		1.2 拿出类型是超级管理员的所有用户
    			ret = models.UserInfo.objects.filter(user_type__caption="管理员").values('user','user_type__caption')
    			
    	2.多对多(ManyToMany)
    		注:创建多台主机,多个部门
    		1.自动创建关系表
    			1.1 h2g =models.ManyToManyField('host')
    				#创建多对多关系
    				1.1.1 将多台机器分配给一个组
    					add添加关系,remove 删除表关系,delete删除表关系和表 set 添加,删除
    					
    					h = Host.objects.get(hid=1) ##只删除表关系
    					h.group_set.remove(*Group.objects.filter(gid__gt=1))
    					
    					h = Host.objects.get(hid=1)   ##添加表关系
    					h.group_set.add(*Group.objects.filter(gid__gt=1))
    					
    					h = Host.objects.get(hid=1)
    					h.group_set.all().delete() #delete删除表关系和表
    					
    					obj =Group.objects.get(gid=1)
    					print(obj.gid,obj.name,obj.h2g.all()) 
    						#bj.h2g.all() 是关系表,没创建关系输出空列表
    					    
    					q =models.objects.filter(hid__gt=3)
    					obj.h2g.add(*q)
    						#将多台主机分到obj组
    						
    				2.2.2 将一台主机分给多个组
    					h =Host.objects.get(hid=1)
    					obj =Group.objects.get(gid=1)
    					obj.h2g.add(h)    ##主机id等于1分配到第一组
    					obj =Group.objects.get(gid=2)  ##
    					obj.h2g.add(h)###主机id等于1分配到第二组
    					
    					h =models.Host.objects.get(hid=1) ##找到第一台主机
    					h.group_set.add(*Group.objects.filter(gid__gt=2))
    						#gid大于2的所有分组分配给H主机
    						
    				反向查找 表名__set ,查找什么表名就是什么
    				h.models.Host.objects.get(hid=1)
    				h.group_set.add(*models.Group.objects.filter(gid__gt=12))
    				
    				注:
    					h = Host.objects.get(hid=1)
    					h.group_set.add(1) ##可以直接写数字添加
    					h.group_set.add(Group.objects.get(gid=1))
    					h.group_set.add(*[1,2,3]) ##可以用列表添加关系
    					h.group_set.set(Group.objects.filter(gid__gt=18), clear=True)
    					clear = True  清空在设置(添加)
    					set 不清空添加
    				注:附加
    					update_or_create,get_or_create 这两个是一样的
    					都是给group和关系表添加数据   前提是关系表里不存在这个数据
    					列:
    				
    					# r = h.group_set.update_or_create(name='技术部')
    					如果没有技术部,就给group组中加上技术部,关系表中自动关联一条数据
    					# print(r)
    					# r = h.group_set.get_or_create(name='人事部')
    					# print(r)
    				
    			
    		2.自己创建表关系
    			HostToGroup.objects.create(Host_id_id=1,group_id_id=2,status=11)
    			#添加内容
    		
    	创建索引
    	 class HostToGroup(models.Model):
    #     hgid = models.AutoField(primary_key=True)
    #     host_id = models.ForeignKey('Host')
    #     group_id = models.ForeignKey('Group')
    #     status = models.IntegerField()
    #     class Meta:
    #         unique_together = [   ###定义唯一索引,里面的索引值就不可以重复
    #             ('host_id', 'group_id'),
    #         ]
    
    
    

      

    实例

    # 1、查询所有图书类型为“科学”的所有书籍的 名称、价格、发布时间、图书类型(两种方式)

    # 2、查询作者“alex"参与编写的所有书籍(两种方式)
    class Author(models.Model):
        '''
        作者
        '''
        name = models.CharField(max_length=100)
        age = models.IntegerField()
    
    class BookType(models.Model):
        '''
        图书类型
        '''
        caption = models.CharField(max_length=64)
    
    class Book(models.Model):
        '''
        图书
        '''
        name = models.CharField(max_length=64)
        pages = models.IntegerField()
        price = models.DecimalField(max_digits=10, decimal_places=2)
        #         数字长度max_digits,有效位数decimal_places
        pubdate = models.DateField()  ##出版事件
        authors = models.ManyToManyField(Author)
        book_type = models.ForeignKey(BookType)
    

    表中添加内容

    # models.Author.objects.create(name='alex',age=12)
        # models.Author.objects.create(name='sunqihu',age=15)
        # models.Author.objects.create(name='jay',age=16)
        #
        # models.BookType.objects.create(caption='文学')
        # models.BookType.objects.create(caption='科学')
        # models.BookType.objects.create(caption='数学')
        #
        # book_mode={"name":"一路向西",
        #            "pages":300,
        #            "price":50,
        #            "pubdate":"2011-09-09",
        #            "book_type_id":2
        #
        #            }
        # models.Book.objects.create(**book_mode)
    

      

    添加关系

     # h =models.Book.objects.get(id=1)
        # h.authors_set.add(models.Author.objects.filter(id=2))
        # h =models.Book.objects.all().get(id=1)
        # obj =models.Author.objects.all().filter(id=3)
        # print(type(obj))
        # # print(h,type(h))
        # obj.book_set.add(h)
    
        # 第一种方法
        # rat = models.Book.objects.filter(book_type__caption='科学').values(
        #     'name','price','pubdate','book_type__caption'
        # )
        # print(rat)
        # for i in rat:
        #     print(i)
        # rem =models.Book.objects.filter(authors__name='sunqihu').values('name','authors__name')
        # print(rem)
    
    
        #第二种方法
    
        # objs =models.BookType.objects.get(caption="科学")
        # obj =objs.book_set.values('name','price','pubdate','book_type__caption')
        # print(obj)
    
    
        # obj = models.Author.objects.get(name='sunqihu')
        # obs =obj.book_set.values('name','authors__name')
        # print(obs)
    

      

  • 相关阅读:
    Excel 实用技巧之一
    Windows操作技巧 之二(持续更新)
    ASCII码表
    Excel 函数VLOOKUP初学者使用指南
    Windows 操作小技巧 之一(持续更新)
    Excel 使用宏批量修改单元格内指定文字为红字
    Excel 使用CHIINV函数和GAMMA.DIST函数绘制卡方分布
    新手使用R的注意事项
    如何在R中加载”xlsx”包
    增值税——基础知识
  • 原文地址:https://www.cnblogs.com/pythonxiaohu/p/5798471.html
Copyright © 2011-2022 走看看