ValueError:基数为10的int()的无效文字:

时间:2022-05-17 22:33:23

i am getting this value error, when i try to insert some data to django model. My python script is :

当我尝试将一些数据插入django模型时,我得到此值错误。我的python脚本是:

from task.employeeDetails.models import EmployeeDetails

def dumpdata():
    userName = "John"    
    designation = 'Software Engineer'
    employeeID = 2312
    contactNumber = 9495321257
    project = 'AOL'
    dateOfJoin = '2009-10-10'    
    EmployeeDetails(userName,designation,employeeID,contactNumber,project,dateOfJoin).save()     
dumpdata() 

My models.py is

我的models.py是

class EmployeeDetails(models.Model):
    userName = models.CharField(max_length=200)
    designation = models.CharField(max_length=200)
    employeeID = models.IntegerField()
    contactNumber = models.CharField(max_length=200)
    project = models.CharField(max_length=200)
    dateOfJoin=models.TextField()

Please help me to solve this error as i am new to python programming. The error is "ValueError: invalid literal for int() with base 10: 'John'

请帮我解决这个错误,因为我是python编程的新手。错误是“ValueError:int()的无效文字,基数为10:'John'

2 个解决方案

#1


5  

I am no Django expert, but try replacing

我不是Django专家,但尝试更换

EmployeeDetails(userName,designation,employeeID,contactNumber,project,dateOfJoin).save() 

with

EmployeeDetails(userName=userName,designation=designation,employeeID=employeeID,contactNumber=contactNumber,project=project,dateOfJoin=dateOfJoin).save() 

#2


7  

Don't use positional arguments, use keywords to specify which field is being populated with what data.

不要使用位置参数,使用关键字指定使用哪些数据填充哪个字段。

EmployeeDetails(userName=userName, designation=designation) #etc

Also, if you are going to call save() anyways, you can use EmployeeDetails.objects.create(...)

另外,如果你打算调用save(),你可以使用EmployeeDetails.objects.create(...)

#1


5  

I am no Django expert, but try replacing

我不是Django专家,但尝试更换

EmployeeDetails(userName,designation,employeeID,contactNumber,project,dateOfJoin).save() 

with

EmployeeDetails(userName=userName,designation=designation,employeeID=employeeID,contactNumber=contactNumber,project=project,dateOfJoin=dateOfJoin).save() 

#2


7  

Don't use positional arguments, use keywords to specify which field is being populated with what data.

不要使用位置参数,使用关键字指定使用哪些数据填充哪个字段。

EmployeeDetails(userName=userName, designation=designation) #etc

Also, if you are going to call save() anyways, you can use EmployeeDetails.objects.create(...)

另外,如果你打算调用save(),你可以使用EmployeeDetails.objects.create(...)