Home  >  Q&A  >  body text

python - django批量导入数据时,如何根据标题或别的字段覆盖原有数据

伪代码如下:

Model.py

class Coupon(models.Model):
    ...
    
    Title=models.CharField('商品标题',max_length=50,null=True,blank=True)
    
    Quan_surplus=models.CharField('优惠券剩余数量',max_length=20,null=True,blank=True)
    
    Quan_receive=models.CharField('已领券数量',max_length=20,null=True,blank=True)
    
    ...
    

项目中用到了一个优惠券的模型,其中优惠券的剩余数量和已领券的数量这个两个字段的内容是会变动的,因为要实时获取最新更新的数据,用Coupon.objects.get_or_create()函数时,因Model中2个字段的内容会变动,导致了批量获取数据并插入时数据库中有很多标题重复但内容就这2个字段不重复的数据,请教有什么办法让最新的数据直接覆盖原有的数据?

伊谢尔伦伊谢尔伦2714 days ago651

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 09:52:38

    Is the product title unique?
    If yes, set Title to unique.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:52:38

    update_or_create() method should meet your needs

    reply
    0
  • Cancelreply