'operator does not exist: character varying[] = text[] in django?
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,red
views.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)
When I am performing this I am getting this error 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.
How to perform this. please needed help.
EDIT:
I want to work with multiple filters at the same time. i.e. price range will be ['300'.'1000'] should be above this list. when performing this I got django.core.exceptions.FieldError: Unsupported lookup 'gt' for PositiveIntegerField or join on the field not permitted, perhaps you meant gt or gte? how can I achieve this?.please add some tips. Thanks.
Solution 1:[1]
Your field colors is text[], the array of strings. You are trying to use IN operator with a list of strings as a parameter. That obviously wouldn't work since elements of text[] is text.
You probably want to filter all products that overlap with given colors. If so, you should use overlap operator:
products = Product.objects.filter(colors__overlap=l)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yevgeniy Kosmak |
