简体   繁体   中英

Why am I getting error: “Value Error” - invalid literal for int() with base 10: 'matthew'

I am using django/python.

Here's where the data comes from: User selects one or more options from a <select name="op_assignedTo">

I also use <option value="username">

The data is sent via POST and collected in a django view:

op_assignedTo = request.POST.getlist('op_assignedTo')

But the following line is give me the error: assignedTo_list = Item.objects.filter(assignedTo__in=op_assignedTo)

I got the above line from numerous other answers to other questions on stackoverflow. I am confused at the error, because even the line

temp = Item.objects.filter(assignedTo='matthew')

gives the same error, "Value Error" - invalid literal for int() with base 10: 'matthew'.

If the first part of my post doesn't quite make sense, please just look at the last line of code I posted. Thanks all!

What kind of field is assignedTo? I'd guess it's a foreign key, in which case it's trying to do the filter on an id, when you're passing a string. Am I right?

The problem here is that assignedTo is being treated as an int (either it is an int, or is being compared on the basis of an int, such as a foreign key id), and you're passing a string to compare it to, which is invalid.

To query a field across a ForeignKey, use double underscore syntax __ .

https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

temp = Item.objects.filter(assignedTo__username='matthew')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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