简体   繁体   English

如何在django中插入批量数据

[英]how to insert bulk data in django

After form submit I am getting data like this.提交表格后,我得到这样的数据。 Now I want to insert this data by the loop in a model or in the table using a for loop.现在我想通过循环将此数据插入模型或使用 for 循环的表中。

{'user_id': ['4'], 
'type': ['1'],
 'csrfmiddlewaretoken': ['Umvdq9BhNUNg94XZNDwkzXfhbjwZY91vKfyqsgBsGAXeRG9NS2DAB87Jvgv3NQlx'], 
 'vehicle_no[]': ['ertere', '11', '2222'], 
 'mileage[]': ['eere', '111', '2222'], 
 'vehicle_category[]': ['1', '1', '1'],
  'status[]': ['2', '2', '2']
}

After this, I am doing this to get the data:在此之后,我这样做是为了获取数据:

for k,v in vals:
   print(vals)

to get values so I can insert it into DB with multiplke rows, but when I am doing the same I am getting the following error:获取值,以便我可以将它插入到具有multiplke行的数据库中,但是当我做同样的事情时,我收到以下错误:

too many values to unpack (expected 2)太多值无法解包(预期 2)

I am stuck here.我被困在这里。 If anyone has an idea please share or solve my issue.如果有人有想法,请分享或解决我的问题。

What you are printing is the whole dictionary in place of key and value pair.您正在打印的是整个字典,而不是键值对。 try using:尝试使用:

for k,v in vals.items():
   print([k,v])

Also, Since you are not changing data from the form, to make things easier, you might consider using the Django forms, it will make things a lot easier for you.此外,由于您没有更改表单中的数据,为了使事情变得更容易,您可以考虑使用 Django 表单,这会让事情变得更容易。

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

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