无法在使用 Django 时增加对象的最新值
Cannot increment object latest value in using django
我想增加 table 中最后一个 id 的值,这样我就可以在下一行输入不同的值。我正在为我的 python 项目使用 oracle 11g 数据库。请看我的示例代码...
custID = Customer.objects.lates('CUSTOMER_ID')
custID = int(custID) + 1 #received error
custName = "Peter"
Customer.objects.create('CUSTOMER_ID'=str(custID), 'CUSTOMER_NAME'=custName) #this line is ok
我得到了这个错误:TypeError: int() argument must be a string, a bytes-like object or a number, not 'Customer'
我尝试在线搜索并更改了我的代码
Customer.objects.create('CUSTOMER_NAME'=custName)
但没有用,因为我的数据库和 python 模型没有自动递增。
我也试过这个..
custID = sum(int(custID), 1)
抱歉,我是 django 的新手。
好吧,如果我理解正确的话,这就是你的问题:
custID = Customer.objects.latest('CUSTOMER_ID')
这个语句会return一个Customer
对象,所以你需要使用
custId = custID.CUSTOMER_ID + 1
Customer.objects.latest('CUSTOMER_ID')
returns 一个对象,不是一个 id。你要的是这个:
custID = Customer.objects.latest('CUSTOMER_ID').id
但是,创建新客户时不需要指定id;通常 Django 会自己获取 id。
我想增加 table 中最后一个 id 的值,这样我就可以在下一行输入不同的值。我正在为我的 python 项目使用 oracle 11g 数据库。请看我的示例代码...
custID = Customer.objects.lates('CUSTOMER_ID')
custID = int(custID) + 1 #received error
custName = "Peter"
Customer.objects.create('CUSTOMER_ID'=str(custID), 'CUSTOMER_NAME'=custName) #this line is ok
我得到了这个错误:TypeError: int() argument must be a string, a bytes-like object or a number, not 'Customer'
我尝试在线搜索并更改了我的代码
Customer.objects.create('CUSTOMER_NAME'=custName)
但没有用,因为我的数据库和 python 模型没有自动递增。
我也试过这个..
custID = sum(int(custID), 1)
抱歉,我是 django 的新手。
好吧,如果我理解正确的话,这就是你的问题:
custID = Customer.objects.latest('CUSTOMER_ID')
这个语句会return一个Customer
对象,所以你需要使用
custId = custID.CUSTOMER_ID + 1
Customer.objects.latest('CUSTOMER_ID')
returns 一个对象,不是一个 id。你要的是这个:
custID = Customer.objects.latest('CUSTOMER_ID').id
但是,创建新客户时不需要指定id;通常 Django 会自己获取 id。