Django独特的约束因复合主键而失败
我搜索了类似的情况,但它们似乎并没有质疑模型。Save()方法。我是Django的新手,遵循标准方法,已声明了将其映射到DDBB(自动启动ID)的类:
class Crudos(models.Model):
cuenta = models.CharField(verbose_name='Cuenta', max_length=100)
concepto = models.CharField(verbose_name='Concepto', max_length=100, default='indefinido')
magnitud = models.FloatField(verbose_name='Magnitud')
fecha = models.DateField(verbose_name='Fecha')
documento = models.FilePathField(verbose_name='Documento')
class Meta:
unique_together = ('cuenta', 'fecha',)
使用此类的视图也填充了表:
for index, row in df.iterrows():
data = Crudos(cuenta=row.Cuenta, concepto=row.Concepto, magnitud=row.Magnitud, fecha=row.Fecha, documento=row.Documento)
data.save()
数据帧是从文件中生成的,并且该过程是从文件中生成的,而无需使用。第一次问题。如果我两次使用相同的文件,那么我会收到唯一约束失败的错误消息。在尝试插入具有相同唯一值组合的寄存器时,Models.Save()应该是更新,而不是插入。当将主键包含在数据中以保存时,它会这样做。当需要复合主键时,问题就到了,因为是crudo类的情况,crudo类是由unique_todecter条件反映的。似乎Django无法将该条件解释为执行更新而不是插入的条件。作为解决该问题的一种方式,该过程可以通过:
for i, row in df.iterrows():
register = Crudos.objects.get(cuenta=row.Cuenta, fecha=row.Fecha)
if register:
data = Crudos(id=register.id, cuenta=row.Cuenta, concepto=row.Concepto, magnitud=row.Magnitud, fecha=row.Fecha, documento=row.Documento)
data.save()
else:
data = Crudos(cuenta=row.Cuenta, concepto=row.Concepto, magnitud=row.Magnitud, fecha=row.Fecha, documento=row.Documento)
data.save()
此过程在每次保存之前都迫使查询,从而大大减慢了过程。还有其他更有效的选择吗?有没有更好的方法使Django自动执行为违反唯一条件的更新情况?
谢谢
I have searched for similar cases but they do not seem to question the model.save() method. I am new to Django and, following the standard methodology, have declared a class to map it to a ddbb (autoincremental id):
class Crudos(models.Model):
cuenta = models.CharField(verbose_name='Cuenta', max_length=100)
concepto = models.CharField(verbose_name='Concepto', max_length=100, default='indefinido')
magnitud = models.FloatField(verbose_name='Magnitud')
fecha = models.DateField(verbose_name='Fecha')
documento = models.FilePathField(verbose_name='Documento')
class Meta:
unique_together = ('cuenta', 'fecha',)
The view that uses this class also populates the table:
for index, row in df.iterrows():
data = Crudos(cuenta=row.Cuenta, concepto=row.Concepto, magnitud=row.Magnitud, fecha=row.Fecha, documento=row.Documento)
data.save()
The dataframe is generated from a file and the process works without problems the first time. If I use the same file twice, then I get the error message of UNIQUE constrain failed. Models.save() should be an update instead of an insert when trying to insert a register with the same unique combination of values. It does so when the primary key is included in the data to save. The problem comes when there is a need for a composite primary key, as it is the case of the Crudo class, which is reflected by the unique_together condition. It seems Django is not able to interpret that condition as one to execute an update instead of an insert. As a way around that problem, the process works by:
for i, row in df.iterrows():
register = Crudos.objects.get(cuenta=row.Cuenta, fecha=row.Fecha)
if register:
data = Crudos(id=register.id, cuenta=row.Cuenta, concepto=row.Concepto, magnitud=row.Magnitud, fecha=row.Fecha, documento=row.Documento)
data.save()
else:
data = Crudos(cuenta=row.Cuenta, concepto=row.Concepto, magnitud=row.Magnitud, fecha=row.Fecha, documento=row.Documento)
data.save()
This process forces a query before each save, slowing down the process significantly. Is there some other more efficient alternative? Is there a better way that allows Django to automatically execute as an update situations that violate the UNIQUE condition?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论