繁体   English   中英

类型错误:只能将元组(不是“int”)连接到元组

[英]TypeError: can only concatenate tuple (not “int”) to tuple

我正在尝试从 db 返回一个值并收到此错误。我在这里尝试了以前回答的问题,但没有运气。有人可以帮助我吗?

@frappe.whitelist()
def generate_barcode():

    last_barcode = frappe.db.sql("""\
        select MAX(barcode) from `tabItem` """)

    if last_barcode:
        last_barcode = last_barcode + 1

    else:
        x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
        random.shuffle(x)
        last_barcode = x[0]



    return {'last_barcode':last_barcode}

添加回溯:

Traceback (innermost last):
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/app.py", line 49, in   application
response = frappe.handler.handle()
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/handler.py", line 66, in handle
execute_cmd(cmd)
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/handler.py", line 89, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/__init__.py", line 531, in   call
return fn(*args, **newargs)
File "/home/adminuser/frappe-bench-hitech/apps/erpnext/erpnext/stock/doctype/item/item.py", line 405, in generate_barcode
last_barcode = last_barcode + 1
TypeError: can only concatenate tuple (not "int") to tuple

我不知道“frappe”是什么,你没有发布完整的回溯,所以我们只能尝试猜测,但很明显frappe.db.sql("select MAX(barcode) from tabItem ")返回一个元组(其中是我对 SQL 数据库的选择所期望的),所以你想要这样的东西:

row = frappe.db.sql(
    "select MAX(barcode) from `tabItem`"
    )

last_barcode = row[0]
if last_barcode:
    last_barcode = last_barcode + 1

附带说明:如果您想要一个介于 0 和 9(包括在内)之间的随机整数,则拼写为random.randint(0, 9)

我不知何故得到了答案。谢谢大家的帮助。

@frappe.whitelist()
def generate_barcode():

last_barcode_auto = frappe.db.sql("""\
    select MAX(barcode) from `tabItem` """)

if last_barcode_auto[0][0] :
    last_barcode = last_barcode_auto[0][0]
    final_barcode= last_barcode+1

else:
    final_barcode=random.randrange(100001, 100000000000, 2)



return {'final_barcode':final_barcode}

错误消息说last_barcode是一个元组。 使用last_barcode[0]检索第一个值(在这种情况下,这将是唯一的值,因为您选择了一个列)。

if last_barcode:
    last_barcode = last_barcode[0] + 1

暂无
暂无

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

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