models.py
from django.contrib.postgres.fields import ArrayField
class Product(DateTimeModel):
colors = ArrayField(models.CharField(max_length=500),null=True, blank=True) # I am having => black,redviews.py
def filters(request):
price_filters = request.GET.getlist('price', default=None) # from form ['300','1000',...]
color_filters = request.GET.getlist('colors', default=None) # from form ['red']
total_filters = list(chain(price_filters,color_filters))
products = Product.objects.filter(Q(colors__in=total_filters) | (Q( price__gt=total_filters)) # I know the gt expecting number but it comes with string and integers. so I get error
print(products)当我执行这个操作时,我得到了这个错误ProgrammingError at /product/filter-query/ operator does not exist: character varying[] = text[] HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
如何完成这个任务。请帮帮忙。
编辑:,我想同时使用多个过滤器。即价格范围为“300”。“1000”应高于此清单。在执行此操作时,我得到了django.core.exceptions.FieldError: Unsupported lookup 'gt' for PositiveIntegerField or join on the field not permitted, perhaps you meant gt or gte?,如何实现this?.please,添加一些提示。谢谢。
发布于 2022-02-22 01:08:28
您的字段colors是text[] (字符串数组)。您正在尝试使用带有字符串列表的IN运算符作为参数。这显然是行不通的,因为text[]的元素是text。
您可能希望过滤所有与给定颜色重叠的产品。如果是这样的话,应该使用overlap operator
products = Product.objects.filter(colors__overlap=l)https://stackoverflow.com/questions/71207193
复制相似问题