Reputation: 83
In my Car model I have the following fields: name, manufacturer, body, car_type and description
.
I've implemented a search bar but when I search it's complaining:
Related Field got invalid lookup: icontains
on Q(car_type__icontains=query)
Whats the issue here exactly?
query = request.GET.get("q")
if query:
car = car.filter(
Q(name__icontains=query) |
Q(manufacturer__icontains=query) |
Q(body__icontains=query) |
Q(car_type__icontains=query)
).distinct()
Models.py
class CarModelBase(models.Model):
class Meta:
abstract = True
name = models.CharField(max_length=100)
class CarType(CarModelBase, SortableModelBase):
class Meta(CarModelBase.Meta):
ordering = ['order']
verbose_name = 'Car Type'
verbose_name_plural = 'Car Types'
def __str__(self):
return self.name
class Car(CarModelBase):
manufacturer = SortableForeignKey(Manufacturer)
car_type = SortableForeignKey(CarType)
body = SortableForeignKey(Body)
description = models.TextField()
Upvotes: 0
Views: 109
Reputation: 309039
If car_type
is a foreign key, then you can't do Q(car_type__icontains=query)
. You need to specify the field on the CarTyoe
model that you wish to search, for example:
Q(car_type__name__icontains=query)
Upvotes: 2