简体   繁体   English

如何从csv文件更新django表中的数据,同时保持主键值不变。

[英]How to update data in django table from csv file while keeping the primary key value as it is.?

I have a table which already contains some data in it. 我有一个已经包含一些数据的表。 Now i want to upload new data from a csv file and want to update some of the previous values of that table. 现在,我想从csv文件上传新数据,并想更新该表的某些先前值。 I am using django 1.3 and sqlite3 as database. 我正在使用django 1.3和sqlite3作为数据库。 But i am not able to update the table. 但是我无法更新表格。

If you do not want to change Primay key values or do not to add new objects to the table, which could be duplicate of the old info - then you need some kind of data which you can use as lookup parameters in your database. 如果您不想更改Primay键值或不想向表中添加新对象(可能与旧信息重复),则需要某种数据,可以将其用作数据库中的查找参数。

If you have model which represents this data, then its really easy using 如果您有代表该数据的模型,那么使用起来真的很容易

m = Model.objects.get(somecolumn = somedata)
m.someothervalue = someotherdata
m.save()

But why include django in this anyway? 但是为什么还要在其中包含django? If you have CSV table, then updating this info is really a case of writing queries. 如果您有CSV表,则更新此信息实际上是编写查询的一种情况。 and programs like Excel and openoffice make this very easy. 像Excel和openoffice这样的程序使此操作非常容易。 If you already have data in CSV format, then just open the data as spreadsheet and use excels/openoffice's Concactenate function to create update queries 如果您已经有CSV格式的数据,则只需将数据作为电子表格打开,然后使用excels / openoffice的Concactenate函数创建更新查询

Update mytable set value1 = data1, value2 = data2 where somevalue = somedata;

If you used openoffice for this, then openoffice has this nifty Text to columns function (under data in program menu), which turns concactenated values into string. 如果您为此使用openoffice,则openoffice具有此漂亮的“文本到列”功能(在程序菜单中的数据下),该功能将固定的值转换为字符串。 Then you can copypaste those strings into command prompt or phppgadmin and run.. and voila, you get updated data in your database. 然后,您可以将这些字符串复制粘贴到命令提示符或phppgadmin中,然后运行..和瞧,您将在数据库中获取更新的数据。

Edit (In response to you comment.): 编辑(针对您的评论。):

Look into this: https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create 看看这个: https : //docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create

If you want to use django for this, then use get_or_create. 如果要为此使用django,请使用get_or_create。 But you need to remember here, that if any of the parameters you use in get_or_create method have changed, then new object will be created. 但是您需要在这里记住,如果您在get_or_create方法中使用的任何参数已更改,则将创建新对象。 Thats why i said in the beginning of the post, that you need some kind of data, which will not change. 这就是为什么我在帖子开头说过,您需要某种数据,这些数据不会改变。

for example (taken from the link above) 例如(摘自上面的链接)

obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon',
                  defaults={'birthday': date(1940, 10, 9)})

will create new obj(Person) when used first time. 首次使用时将创建新的obj(Person)。 But if used 2nd time and the date has changed, then new Person with same name and last name but new date will be created. 但是如果第二次使用并且日期已更改,则将创建具有相同名称和姓氏但新日期的新人员。

So to avoid this, you'll still need to do something like 因此,为避免这种情况,您仍然需要做类似的事情

obj.someothervalue = someotherdata
obj.save()

if you want to have more control over the data, that could have been changed. 如果您想对数据有更多的控制权,则可以进行更改。

Alan. 艾伦

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

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