【发布时间】:2021-02-08 06:28:19
【问题描述】:
我有以下函数,它应该接受 3 个参数(标题、描述或产品 ID)。前两个是字符串,第三个是整数值。
# Create the necessary search function for assignment
def search(request): # Get all products below where title, desc or product ID is in the query
query = request.GET.get('query')
products = Product.objects.filter(Q(title__icontains=query) | Q(description__icontains=query) | Q(product_id=query))
context = {
'query': query,
'products': products
}
上述函数抛出错误:
ValueError at /search/
Field 'product_id' expected a number but got 'playstation'.
Request Method: GET
Request URL: http://localhost:8000/search/?query=playstation
Django Version: 3.1.2
Exception Type: ValueError
Exception Value:
Field 'product_id' expected a number but got 'playstation'.
搜索栏将只接受整数值。是否可以在 Q 对象中包含同时使用字符串或整数的选项?我对 MySQL 语法很陌生。
【问题讨论】:
标签: python search django-models integer valueerror