原代码:
class DictConfig(models.Model): """ 字典表 """ id = models.AutoField(primary_key=True) dictype= models.CharField("类型", max_length=30, unique=True) dicdesc = models.CharField("描述", max_length=30) dicremarks = models.CharField("备注", max_length=50, null=True, blank=True) create_time = models.DateTimeField('创建时间', auto_now_add=True, null=True, blank=True) update_time = models.DateTimeField('更新时间', auto_now=True) 修改数据部分代码: dic_serializer = DictConfigSerializer(data=data) with transaction.atomic(): save_id = transaction.savepoint() if dic_serializer.is_valid(): try: dic = DictConfig.objects.get(id=id) dic_serializer.update(instance=dic, validated_data=data) except Exception as e: transaction.savepoint_rollback(save_id) return JsonResponse(code=1, msg=e)
执行后一直提示该数据已存在,实际未修改任何数据,只是做了个提交操作
结果
修改代码即可:
class DictConfig(models.Model): """ 字典表 """ id = models.AutoField(primary_key=True) dictype= models.CharField("类型", max_length=30) # 去掉unique=True
dicdesc = models.CharField("描述", max_length=30)
dicremarks = models.CharField("备注", max_length=50, null=True, blank=True)
create_time = models.DateTimeField('创建时间', auto_now_add=True, null=True, blank=True)
update_time = models.DateTimeField('更新时间', auto_now=True)
标签:models,数据,XXX,length,max,CharField,True,id,字典 From: https://www.cnblogs.com/mian-1122/p/16625483.html