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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With