我正在尝试查找与特定campus_id
匹配的数据库。如果我使用整数,则可使用此功能,但不适用于字符串。
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 (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'
答案 0 :(得分:1)
我在错误消息中看到您试图在字段dcu alpha
(这是一个Restaurant.campus_id
中搜索值ForeignKey
的原因,因此在数据库中它将具有以下类型:模型Campus
的PK,它是一个整数)。 Django尝试将搜索值转换为int
,因为该字段是整数。
也许您想搜索其他字段,也许:
name
(Restaurant
实例的名称)campus_id__name
(属于Campus
实例的Restaurant
实例的名称)?或者您可以添加错误处理:
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)