过滤以搜索数据库中的项目
Filtering to search for items in database
filters.py
import django_filters
from .models import *
class ProductFilter(django_filters.FilterSet):
class Meta:
model=Product
fields=['name',]
产品 class models.py
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.FloatField()
digital = models.BooleanField(default=False,null=True, blank=True)
image=models.ImageField(null=True, blank=True)
description=models.CharField(max_length=400, default='SOME STRING', null=True)
models.py
中的 class 订单项
class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
views.py
def store(request):
if request.user.is_authenticated:
customer = request.user.customer
# Returns a tuple of (object, created),
# where object is the retrieved or created object and
# created is a boolean specifying whether a new object was created.
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems=order.get_cart_items
else:
#Create empty cart for now for non-logged in user
items = []
order = {'get_cart_total':0, 'get_cart_items':0}
cartItems=order['get_cart_items']
products = Product.objects.all()
myFilter=ProductFilter(request.GET, queryset=items)
items=myFilter.qs
context = {'products':products, 'cartItems': cartItems, 'myFilter': myFilter}
return render(request, 'store/store.html', context)
模板文件(main.html)
<form class="form-inline mr-auto my-lg-0" method="get">
<input class="form-control mr-sm-2" style="width:1000px; height:38px;" type="text" placeholder="Search for items" aria-label="Search">
{{myFilter.form}}
<button class="btn btn-outline-warning btn-rounded btn-sm my-0" style="width:80px; height:38px" type="submit">Search</button>
</form>
FieldError at /
Cannot resolve keyword 'name' into field. Choices are: date_added, id, order, order_id, product, product_id, quantity
请求方法:GET
请求URL:http://127.0.0.1:8000/?name=Fifa+20
Django 版本:2.2
异常类型:FieldError
异常值:
无法将关键字 'name' 解析为字段。选项有:date_added、id、订单、order_id、产品、product_id、数量
异常位置:D:\ecom\venv\lib\site-packages\django\db\models\sql\query.py in names_to_path, line 1420
Python 可执行文件:D:\ecom\venv\Scripts\python.exe
Python版本:3.6.5
在我使用目录中的一种有效产品进行搜索后,它显示了上述错误。
我从中推断出两件事。
1. name is a valid field in Product class in models.py
2. the other fields(date_added, id, order etc) mentioned in the image are fields of OrderItem class in models.py
但由于我要搜索产品目录,所以filters.py中的型号字段应设置为'name'下有效字段的产品。
我想解决这个问题,这样我就可以在我的商店中搜索商品了。我应该如何修改我的代码?
ProductFilter 指向产品型号。
但是在这里,您使用 items
作为 ProductFilter
的查询集
myFilter=ProductFilter(request.GET, queryset=items)
但项目包含 OrderItem 模型的查询集-
items = order.orderitem_set.all()
因此它显示了来自 OrderItem 模型而非产品模型的过滤器的可用选项列表。
filters.py
import django_filters
from .models import *
class ProductFilter(django_filters.FilterSet):
class Meta:
model=Product
fields=['name',]
产品 class models.py
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.FloatField()
digital = models.BooleanField(default=False,null=True, blank=True)
image=models.ImageField(null=True, blank=True)
description=models.CharField(max_length=400, default='SOME STRING', null=True)
models.py
中的 class 订单项class OrderItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
views.py
def store(request):
if request.user.is_authenticated:
customer = request.user.customer
# Returns a tuple of (object, created),
# where object is the retrieved or created object and
# created is a boolean specifying whether a new object was created.
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems=order.get_cart_items
else:
#Create empty cart for now for non-logged in user
items = []
order = {'get_cart_total':0, 'get_cart_items':0}
cartItems=order['get_cart_items']
products = Product.objects.all()
myFilter=ProductFilter(request.GET, queryset=items)
items=myFilter.qs
context = {'products':products, 'cartItems': cartItems, 'myFilter': myFilter}
return render(request, 'store/store.html', context)
模板文件(main.html)
<form class="form-inline mr-auto my-lg-0" method="get">
<input class="form-control mr-sm-2" style="width:1000px; height:38px;" type="text" placeholder="Search for items" aria-label="Search">
{{myFilter.form}}
<button class="btn btn-outline-warning btn-rounded btn-sm my-0" style="width:80px; height:38px" type="submit">Search</button>
</form>
FieldError at / Cannot resolve keyword 'name' into field. Choices are: date_added, id, order, order_id, product, product_id, quantity
请求方法:GET
请求URL:http://127.0.0.1:8000/?name=Fifa+20
Django 版本:2.2
异常类型:FieldError
异常值:
无法将关键字 'name' 解析为字段。选项有:date_added、id、订单、order_id、产品、product_id、数量
异常位置:D:\ecom\venv\lib\site-packages\django\db\models\sql\query.py in names_to_path, line 1420
Python 可执行文件:D:\ecom\venv\Scripts\python.exe
Python版本:3.6.5
在我使用目录中的一种有效产品进行搜索后,它显示了上述错误。 我从中推断出两件事。
1. name is a valid field in Product class in models.py
2. the other fields(date_added, id, order etc) mentioned in the image are fields of OrderItem class in models.py
但由于我要搜索产品目录,所以filters.py中的型号字段应设置为'name'下有效字段的产品。
我想解决这个问题,这样我就可以在我的商店中搜索商品了。我应该如何修改我的代码?
ProductFilter 指向产品型号。
但是在这里,您使用 items
作为 ProductFilter
myFilter=ProductFilter(request.GET, queryset=items)
但项目包含 OrderItem 模型的查询集-
items = order.orderitem_set.all()
因此它显示了来自 OrderItem 模型而非产品模型的过滤器的可用选项列表。