Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Related Field got invalid lookup: icontains - Django 2.1

I am trying to make the "vanKit" field searchable in my admin page. "vanKit" is a ForeignKey and whenever I add it it my search_fields list it gives me this error "Related Field got invalid lookup: icontains". Here's my code:

Models.py

class KitSupplies(models.Model):
    supplyName = models.ForeignKey(supplies, on_delete=models.CASCADE)
    vanKit = models.ForeignKey(van_kit, on_delete=models.CASCADE)
    quantity = models.PositiveSmallIntegerField(blank=False)
    def __str__(self):
        return str(self.supplyName)
    class Meta:
        verbose_name_plural = 'Kit Supplies'

admin.py

class KitSuppliesAdmin(admin.ModelAdmin):
    list_display = ('supplyName', 'vanKit', 'quantity')
    search_fields = ['vanKit']

admin.site.register(KitSupplies, KitSuppliesAdmin)

I tried to use search_fields = ['vanKit__name'] like the other stack overflow answers suggested but that did not work for me. Can anyone explain why I am getting this error and how to get around it? I am using Django 2.1 and python 3.7. Thanks in advance!

like image 896
Tank12 Avatar asked Mar 09 '19 01:03

Tank12


1 Answers

So I figured it out, I had to use "search_fields = ['vanKit__van_kit_name']" van_kit_name is the field that held the name of the van kits in the van_kit model that "vanKit" relates to through a Foreign Key. I had to access the Char Field that held the van kit's name in the actual van_kit model.

like image 105
Tank12 Avatar answered Oct 22 '22 11:10

Tank12