Reputation: 177
I'm trying lookup the database that match specific campus_id
. This works if I use integers, but doesn't work with strings.
models.py
from django.db import models
class Campus(models.Model):
campus_id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Restaurant(models.Model):
restaurant_id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
location = models.CharField(max_length=100)
campus_id = models.ForeignKey(Campus,on_delete = models.CASCADE)
opening_hours = models.TimeField()
closing_hours = models.TimeField()
capacity = models.IntegerField()
#staff = models.IntegerField()
def __str__(self):
return self.name
views.py
def restaurants(request):
query = request.GET.get('search', None)
context = {}
if query and request.method == 'GET':
results = Restaurant.objects.filter(campus_id=query)
context.update({'results':results})
return render(request, 'eatatdcu/restaurants.html', context)
restaurants.html
{% for x in results %}
{{x.name}} , {{x.location}}
<br>
{% endfor %}
Traceback log
Traceback (most recent call last):
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Calvin\Desktop\CA377\2019-ca377-EatAtDCU\src\ca377\eatatdcu\views.py", line 13, in restaurants
results = Restaurant.objects.filter(campus_id=query)
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 781, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\query.py", line 799, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\query.py", line 1260, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\query.py", line 1286, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\query.py", line 1216, in build_filter
condition = lookup_class(lhs, value)
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\lookups.py", line 24, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related_lookups.py", line 110, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "C:\Users\Calvin\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line 1849, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'dcu alpha'
Upvotes: 0
Views: 806
Reputation: 16495
I see in the error message that you tried to search for the value dcu alpha
in the field Restaurant.campus_id
(which is a ForeignKey
, so in the database it will have the type of the PK of the model Campus
, which is an integer). Django tries to cast the search value to an int
because the field is an integer.
Maybe you want to search a different field, maybe:
name
(the name of the Restaurant
instances)campus_id__name
(the name of the Campus
instance belonging to the Restaurant
instances)?Or you could add error handling:
def restaurants(request):
query = request.GET.get('search', None)
context = {}
if query and request.method == 'GET':
try:
query_int = int(query)
except ValueError:
query_int = -1
results = Restaurant.objects.filter(campus_id=query_int)
context.update({'results':results})
return render(request, 'eatatdcu/restaurants.html', context)
Upvotes: 1