简体   繁体   English

现有行更新或使用Django模型插入新行

[英]Existing row update or else insert a new row using django models

I have a table where i am checking if any row exists, if yes then update some fields of that table and if no, then particular row is inserted with all the values of the specified fields using django models.. 我有一张表,我正在检查是否存在任何行,如果是,则更新该表的某些字段,如果不存在,则使用Django模型将特定行插入指定字段的所有值。

So basically what i have done is.. First i check if the values doesn't exist, then i add those values using create method.. After that i read the table again and then check whether to update particular field or not after all this i send a jsonresponse.. 所以基本上我所做的是..首先,我检查值是否不存在,然后使用create方法添加这些值。.之后,我再次读取表,然后检查是否更新所有特定字段我发送一个jsonresponse ..

So when i request first time it adds all the rows required along with the existing one and gives an value error saying httpresponse error returned none and again if i request same url i get the values with updated fields too based on the inserted rows as well! 因此,当我第一次请求时,它将所有所需的行与现有行一起添加,并给出值错误,表明httpresponse错误未返回,如果我请求相同的url,我也将根据插入的行获得具有更新字段的值!

So is their a way to make it work as if the row doesn't exist then insert or else update specific field? 那么他们是一种使它工作的方法,好像该行不存在然后插入还是更新特定字段的方法?

按照您的逻辑进行两步处理:1.检查记录是否存在:是:更新编号:创建新记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM