python - 如何在 Django batch_create 操作中进行 on_conflict_replace 工作?
问题描述
该领域account
和month
独特的在一起。而且我正在尝试使用数据库batch_insert
数据来加快我的数据库操作。bulk_create
Django
我的数据库后端mysql
是innodb
下面是我的基表
# my db table
class MSpendForMonth(models.Model):
created_time = models.DateTimeField(auto_now_add=True)
updated_time = models.DateTimeField(auto_now=True)
system_value = models.DecimalField(max_digits=16, decimal_places=2)
checked_value = models.DecimalField(max_digits=16, decimal_places=2, null=True)
account = models.ForeignKey(MAccount,
models.DO_NOTHING,
related_query_name="account_month",
related_name="account_month"
)
month = models.IntegerField(blank=True, null=True)
class Meta:
db_table = 'spend_for_month'
unique_together = (('account', 'month'),)
# db operation
bulk_data = []
try:
with transaction.atomic():
for spend_data in spend_month_date_sum_queryset:
bulk_data.append(
MSpendForMonth(
account_id=spend_data["account_id"],
month=month,
system_value=spend_data["sum_value"],
)
)
MSpendForMonth.objects.bulk_create(bulk_data, ignore_conflicts=True )
# MSpendForMonth.objects.bulk_update(bulk_data, fields="system_value")
except:
import traceback as tb
tb.print_exc()
我想system_value
用一个新值替换,因为它有时会改变。
我的问题是,如果唯一键上存在冲突,我该如何bulk_create
数据并替换为新值。system_value
account-month
它应该类似于insert_many().on_conflict_replace() -- peewee
太谢谢了。
解决方案
推荐阅读
- azure - 无法让 cloudtor:azure Docker 插件与最新版本的 Docker/插件一起使用
- mysql - 如何根据逻辑自查表并添加列?
- java - 为什么赋值运算符链过程以从右到左的顺序进行
- reactjs - 通过 netlify 部署反应应用程序时出现问题 - 环境变量
- authentication - 如何找出单点登录从 Google 和 Facebook 收集了哪些数据?
- angular - API Rest wordpress 上的 Angular HttpClient
- reactjs - ReactJS+Typescript:为什么React.Component中需要State类型
- android - 如何从 Activity 2 imageview2 中的用户选择在 Activity1 中显示相同的 imageview1?
- r - 从存储为字符的时间变量中获取时间差
- javascript - 预期的间谍点击已被调用