Django批量更新多个属性

有时候我们需要同时(一次性)更新某个用户的多条属性。

1. 用户model如下:

      class User(models.Model):
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      UID = models.CharField('员工uid', max_length=200,)
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      name = models.CharField('员工名字', max_length=200,)
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      mobile = models.CharField('手机号', max_length=200,)
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      mail = models.EmailField(u'邮箱', max_length=200)
     
       
     
     
     
    
      
    
    
    
   
     
   
   
   

2. 用户的数据

      user_info = {'UID': 'ADBES682BOEO',
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      'name': '张三',
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      'mobile': '12345678911',
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      'mail': 'test@test.com'
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      }
     
       
     
     
     
    
      
    
    
    
   
     
   
   
   

3. 新建用户

      User.object.create(UID='ADBES682BOEO',name='张三',mobile='12345678911',mail='test@test.com')
     
       
     
     
     
    
      
    
    
    
   
     
   
   
   

这就会在数据库中新建一个张三的数据。

4. 更新数据

      user_info = {'UID': 'ADBES682BOEO',
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      'name': '张三2',
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      'mobile': '12345678912',
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      'mail': 'test2@test.com'
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      }
     
       
     
     
     
    
      
    
    
    
   
     
   
   
   
4.1 一般的更新操作
      user = User.object.get(UID='ADBES682BOEO')
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      user.name = user_info['name']
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      user.mobile = user_info['mobile']
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      user.mail = user_info['mail']
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      user.save()
     
       
     
     
     
    
      
    
    
    
   
     
   
   
   
4.2 批量操作
      user = User.object.filter(UID='ADBES682BOEO')
     
       
     
     
     
     
       
     
     
     
      
        
      
      
      
      
        
      
      
      user.update(**user_info)
     
       
     
     
     
    
      
    
    
    
   
     
   
   
   

欢迎大家访问我的博客:bigyoung.cn:10084

如果觉得有用,麻烦您点击一下『在看』,这是我努力的源泉!

本文分享自微信公众号 - BigYoung小站(bigyoungs)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

评论

暂无评论