zoukankan      html  css  js  c++  java
  • django使用bulk_update批量更新数据

    一、第一步,导入bulk_update包
    from bulk_update.helper import bulk_update
    二、写一个类
    class WestMedicineSkuQuerySet(models.QuerySet):
        def latest_records(self):
            return self.filter(is_deleted=False)
    
        def bulk_update(self, objs, update_fields=None,
                        exclude_fields=None, batch_size=None):
            self._for_write = True
            using = self.db
    
            return bulk_update(
                objs, update_fields=update_fields,
                exclude_fields=exclude_fields, using=using,
                batch_size=batch_size)
    

      

    三、第三步,在需要使用批量更新的model类中,添加一行代码
    class Track(models.Model):
        album = models.ForeignKey(Album, related_name='tracks', on_delete=models.CASCADE)
        order = models.IntegerField()
        title = models.CharField(max_length=100)
        duration = models.IntegerField()
         objects = WestMedicineSkuQuerySet.as_manager()
    
        
        class Meta:
            unique_together = ('album', 'order')
            ordering = ['order']
    

      


  • 相关阅读:
    Beans
    HDU2830
    HDU1176
    rtc关机闹钟7 jni层 com_android_server_AlarmManagerService
    rtc关机闹钟6 AlarmManagerService研究
    rtc关机闹钟5 AlarmManager研究
    数论学习_裴蜀定理
    hdu-5465-二维BIT+nim
    hdu-1892-二维BIT
    hdu-2227-dp+bit
  • 原文地址:https://www.cnblogs.com/ltyc/p/15261608.html
Copyright © 2011-2022 走看看