You want to search on the author's author_name
field, not the id.
Quote.objects.filter(author__author_name = name)
aquotelist = Quote.objects.filter(author__author_name__exact = name)
Try changing the corresponding line to the above. The way you have it now, you are matching author
to the given name, but author
is probably considered by its ID here, definitely not by its author_name
. The format is as follows:
Quote.objects.filter([model] __[field] __exact = [whatever])
I have faced and solved similar issue,
focus on attribute : author = models.ForeignKey(Author)
django expect that you pass an Instance of Author
class to the author
field. When you search django want to get a id (a integer number of this Instance):
so to get :
aquotelist = Quote.objects.filter(author__exact = name)
This will fix your problem permanently ::go to your directory
C: \python 37\ Lib\ site - packages\ django\ db\ models\ fields
and edit the file __init__.py
and edit line 1807; replace
return int(value)
with
return int()
I don’t think anything without being able to see the complete traceback of the error and the code associated with that error.,Hi all! I have model Employee and same table in my local database. I need to have the possibility to edit any record and save it locally. When I tried to edit record with actual id I got this error: invalid literal for int() with base 10: ‘undefined’ . I made a research but can’t really find something helpful in my case. I have troubles with this from a few days so thanks a lot I hope my question is clear hope someone can help ,Please post the complete traceback of the error you’ve received. It’s hard to locate a problem like this without knowing where in the code that exception is being thrown.,int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method’
my edit function in views.py:
def staff_edit(request, id = 0):
#employees = Employee.objects.all()
#print(employees)
if request.method == 'GET':
if id == 0:
form = EmployeeEditForm()
else:
employees = Employee.objects.get(pk = id)
form = EmployeeEditForm(instance = employees)
return render(request, 'staffedit.html', {
'form': form
})
else:
if id == 0:
form = EmployeeEditForm(request.POST)
else:
employees = Employee.objects.get(pk = id)
form = EmployeeEditForm(request.POST, instance = employees)
if form.is_valid():
form.save()
return redirect('feedback:index_employees')
context = {
'form': form
}
#when the form is invalid
return render(request, 'staffedit.html', context)
this is my model.py for Employee:
class Employee(models.Model):
coreapiemployee_id = models.CharField(max_length = 100)
webflow_id_employee = models.CharField(max_length = 100,
default = True)
first_name = models.CharField(max_length = 100)
last_name = models.CharField(max_length = 100,
default = True)
email = models.EmailField(max_length = 100)
user_type = models.CharField(max_length = 100)
status = models.CharField(max_length = 100,
default = True)
roles = models.ManyToManyField('Role', through = 'EmployeeRole')
def __str__(self):
return self.coreapiemployee_id + " " + self.email + self.first_name + self.last_name
this is my general html staff.html:
$(document).ready(function() {
var data;
fetch("http://192.168.2.85:8000/displayEmployeesToFroentend/")
.then(response => response.json())
.then(json => data = json)
.then(() => {console.log(data);
let employees = JSON.parse(data.employees_json);
var table = $('#datatable').DataTable( {
data: employees,
select: "single",
"columns": [
{ "data": "fields.coreapiemployee_id"},
{ "data": "pk"},
{ "data": "fields.first_name" },
{ "data": "fields.last_name" },
{ "data": "fields.email" },
{ render: function ( data, type, row ) {
return '<a href="http://192.168.2.85:8000/staff/edit/' + row.coreapiemployee_id + '"><i class="far fa-edit fa-lg" aria-hidden="true"></i></a>';
} },
{ render: function ( data, type, row ) {
return '<i class="fa fa-plus-circle" aria-hidden="true"></i>';
} },
],
"order": [[1, 'asc']]
} )
})
} );
my forms.py for EmployeeEditForm:
class EmployeeEditForm(ModelForm):
class Meta:
model = Employee
fields = [
'coreapiemployee_id',
'first_name',
'last_name',
'roles',
urls.py:
path('staff/edit/<str:id>', views.staff_edit, name="staffedit"),
Internal Server Error: /staff/edit / 6
Traceback(most recent call last):
File "/usr/lib/python3/dist-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/lib/python3/dist-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/lib/python3/dist-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, * callback_args, ** callback_kwargs)
File "/home/stela/feedbacksystem/feedback/views.py", line 288, in staff_edit
employees = Employee.objects.get(pk = id)
File "/usr/lib/python3/dist-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)( * args, ** kwargs)
File "/usr/lib/python3/dist-packages/django/db/models/query.py", line 399, in get
clone = self.filter( * args, ** kwargs)
File "/usr/lib/python3/dist-packages/django/db/models/query.py", line 892, in filter
return self._filter_or_exclude(False, * args, ** kwargs)
File "/usr/lib/python3/dist-packages/django/db/models/query.py", line 910, in _filter_or_exclude
clone.query.add_q(Q( * args, ** kwargs))
File "/usr/lib/python3/dist-packages/django/db/models/sql/query.py", line 1290, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/usr/lib/python3/dist-packages/django/db/models/sql/query.py", line 1315, in _add_q
child_clause, needed_inner = self.build_filter(
File "/usr/lib/python3/dist-packages/django/db/models/sql/query.py", line 1251, in build_filter condition = self.build_lookup(lookups, col, value) File "/usr/lib/python3/dist-packages/django/db/models/sql/query.py", line 1116, in build_lookup lookup = lookup_class(lhs, rhs) File "/usr/lib/python3/dist-packages/django/db/models/lookups.py", line 20, in __init__ self.rhs = self.get_prep_lookup() File "/usr/lib/python3/dist-packages/django/db/models/lookups.py", line 70, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "/usr/lib/python3/dist-packages/django/db/models/fields/__init__.py", line 972, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes - like object or a number, not 'builtin_function_or_method'
Added the face_png foreign key and not migrate fails with ValueError: invalid literal for int() with base 10: 'None, remove the foreign key from ArchBase and migrate works as expected. , Unless ForeignKey.to_field is specified your ArchBase.face_png field will point to the FacePng.id which is the implicit primary key when none is explicitly specified. This field is an auto-incrementing integer hence why int('None') is attempted. , You either want to explicitly declare one your FacePng fields primary key or make one them unique and adjust ArchBase.face_png(to_field) to point to it. Please see one of the support channels if you need further assistance. ,© 2005-2022 Django Software Foundation unless otherwise noted. Django is a registered trademark of the Django Software Foundation.
Added the face_png foreign key and not migrate fails with ValueError: invalid literal for int() with base 10: 'None, remove the foreign key from ArchBase and migrate works as expected.
class FacePng(models.Model):
arc_filename = models.CharField(max_length = 128, blank = True, null = True)
png = models.CharField(max_length = 128, blank = True, null = True)
def __str__(self):
return self.png
class FacePng(models.Model): arc_filename = models.CharField(max_length=128, blank=True, null=True) png = models.CharField(max_length=128, blank=True, null=True) def __str__(self): return self.png
class ArchBase(models.Model):
face_png = models.ForeignKey(
'FacePng',
on_delete = models.CASCADE,
blank = True,
null = False,
default = "None",
)
If base is specified, then int() function tries to convert the given parameter to an integer in the given base.,Also, the default base to convert to is base 10. ,If a float is passed to int() function then it returns the truncated value.,If you are trying to convert a float string to an integer, you need to first convert it to float and then to an integer.
Trying to convert a string to an integer.
rana@brahma:~$ python3
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(int("dawd"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'dawd'
Trying to convert a float string to an integer.
>>> print(int("110.0"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '110.0'
>>>
Trying to convert an empty string to an integer.
>>> print(int(""))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>>
The default parameter is 0 which is also the default return value if no value is passed.
>>> print(int()) 0 >>>
Also, the default base to convert to is base 10.
int(x, base = 10)
The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function . In other words it's either empty, or has a character in it other than a digit.,The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function . In other words it's either empty, or has a character in it other than a digit. ,I want to bring one alternate perspective to focus on why does this error happening? What does invalid literal for int with base 10 mean in Python? ,Sometimes the difference between Python2.x and Python3.x that leads to this ValueError: invalid literal for int() with base 10 .
I am creating a program that reads a file and if the first line of the file is not blank, it reads the next four lines. Calculations are performed on those lines and then the next line is read. If that line is not empty it continues. However, I am getting this error:
ValueError: invalid literal
for int() with base 10: ''.
`
Here is the code as explained by @ Nymeria:
>>> int('5')
5
>>> float('5.0')
5.0
>>> float('5')
5.0
>>> int(5.0)
5
>>> float(5)
5.0
>>> int('5.0')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '5.0'
>>> int(float('5.0'))
5
Just for the record:
>>> int('55063.000000')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '55063.000000'
Got me here...
>>> int(float('55063.000000'))
55063.0
We can get this error when trying to convert a variable to an integer.,If base is specified, then int() function tries to convert the given parameter to an integer in the given base.,If a float is passed to int() function then it returns the truncated value.,The default parameter is 0 which is also the default return value if no value is passed.
Trying to convert a string to an integer.
rana@brahma:~$ python3
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(int("dawd"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'dawd'
Trying to convert a float string to an integer.
>>> print(int("110.0"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '110.0'
>>>
Trying to convert an empty string to an integer.
>>> print(int(""))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>>
The default parameter is 0 which is also the default return value if no value is passed.
>>> print(int()) 0 >>>
Also, the default base to convert to is base 10.
int(x, base = 10)
Passing int() a string-type object which looks like a float (e.g. '56.9'), will also trigger the ValueError.,When passing int() a string-type object which looks like a float-type (e.g., the string '56.3'). Although technically, this is an extension of the first error case, Python recognizes the special character .inside the string '56.3'.,In cases where we'd like to convert a string-type object that looks like float (e.g. '56.3') into an integer, we will also trigger the error. The error happens due to the presence of the .character. We can easily avoid the error by converting the string to a floating-point object first, as follows:,int() works with floats, so the simplest way to fix the error, in this case, is to convert our string to a floating-point number first. After converting the string-type to float-type, we can successfully pass our object to the int() function:
ValueError: invalid literal
for int() with base 10:
Learn Data Science with
val_1 = input("Enter the first value: ")
val_2 = input("Enter the second value: ")
new_val = val_1 + val_2
print("\nThe sum is: ", new_val)
Learn Data Science with
Enter the first value: 5
Enter the second value: 3
Learn Data Science with
The sum is: 53
Learn Data Science with
val_1 = input("Enter the first value: ")
val_2 = input("Enter the second value: ")
new_val = int(val_1) + int(val_2)
print("\nThe sum is: ", new_val)
Learn Data Science with
The sum is: 8
Learn Data Science with