gpt4 book ai didi

mysql - Django无效文字错误阻止数据库插入

转载 作者:行者123 更新时间:2023-11-29 22:11:01 25 4
gpt4 key购买 nike

我正进入(状态:

data= u'{"1":"Mubashir","2":"your@email.com","3":"9995735310","4":"headrun","5":"kannur","6":"in","7":"1","8":"2","9":"3"}'


通过在我的views.py中获取请求。
我想将前6个数据保存到一个名为customer的表中,然后将包含另外一个称为answer的表的主键的下3个数据(7,8和9)用于将新保存的客户存储为Answer表的外键

这是代码:

def customer(request):
answer=request.GET['data']
jsonData=json.loads(answer)
entry = Customer(name = jsonData["1"], email = jsonData["2"], phone = jsonData["3"], company=jsonData["4"], city=jsonData["5"], country=jsonData["6"])
entry.save();

for key in sorted(jsonData)[:6]:
a=int(jsonData[key])
answer=Answer.objects.get(pk=a);
answer=Answer(customer=entry);
answer.save();


客户表已正确填写。我在映射主键值时遇到问题。通过以下错误显示:

invalid literal for int() with base 10: 'Mubashir'
Request Method: GET
Request URL: http://fundoocode.ninja:8012/customer?data=%7B%221%22%3A%22Mubashir%22%2C%222%22%3A%22your%40email.com%22%2C%223%22%3A%229995735310%22%2C%224%22%3A%22headrun%22%2C%225%22%3A%22kannur%22%2C%226%22%3A%22in%22%2C%227%22%3A%221%22%2C%228%22%3A%222%22%2C%229%22%3A%223%22%7D
Django Version: 1.8.3
Exception Type: ValueError
Exception Value: invalid literal for int() with base 10: 'Mubashir'
Exception Location: /home/spartan313/proj/feedback/feedbackcollection/views.py in customer, line 63
Python Executable: /home/spartan313/proj/bin/python
Python Version: 2.7.9
Python Path:
['/home/spartan313/proj/feedback','/home/spartan313/proj/lib/python2.7','/home/spartan313/proj/lib/python2.7/plat-x86_64-linux-gnu','/home/spartan313/proj/lib/python2.7/lib-tk','/home/spartan313/proj/lib/python2.7/lib-old','/home/spartan313/proj/lib/python2.7/lib-dynload','/usr/lib/python2.7','/usr/lib/python2.7/plat-x86_64-linux-gnu','/usr/lib/python2.7/lib-tk','/home/spartan313/proj/local/lib/python2.7/site-packages','/home/spartan313/proj/lib/python2.7/site-packages']

最佳答案

您正在访问前6个而不是从6个开始到最后一个

for key in sorted(jsonData)[:6]:


这个循环会给你

1
2
3
4
5
6


应该

for key in sorted(jsonData)[6:]:


这个会给你

7
8
9

关于mysql - Django无效文字错误阻止数据库插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31667662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com