0

I am getting this error django.core.exceptions.FieldError: Unsupported lookup 'icontains' for ForeignKey or join on the field not permitted

Below is models.py:

class Author(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    
    def __str__(self):
        return self.first_name + ' ' + self.last_name
    
class Book(models.Model):
    title = models.CharField(max_length=100)
    rating = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(5)])
    author = models.ForeignKey(Author,on_delete=models.CASCADE,null=True)
    is_bestselling = models.BooleanField(default=False)
    slug = models.SlugField(default="",null=False,blank=True)

    def get_absolute_url(self):
        return reverse("model_detail", args=[self.slug])    
    
    def __str__(self):
        return self.title

Below is admin.py:

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'rating', 'author','is_bestselling',)
    list_display_links = ('title', 'rating',)
    search_fields = ('title', 'rating','author',)
    list_filter = ('rating', 'is_bestselling',)
    prepopulated_fields = {
        'slug': ('title',)
    }

class AuthorAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name',)
    list_display_links = ('first_name', 'last_name',)
    search_fields = ('first_name', 'last_name',)

I was trying to search Author in my Books Model.

2 Answers2

0

This is answer below :

To resolve this issue, you can modify the search_fields attribute to search on related fields of the Author model instead. Here's an updated version of my BookAdmin class:

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'rating', 'author', 'is_bestselling',)
    list_display_links = ('title', 'rating',)
    search_fields = ('title', 'rating', 'author__first_name', 'author__last_name',)
    list_filter = ('rating', 'is_bestselling',)
    prepopulated_fields = {
        'slug': ('title',)
    }

In the modified search_fields, 'author__first_name' and 'author__last_name' are used to search on the first_name and last_name fields of the related Author model. The __ syntax is used to traverse the relationship between Book and Author models.

By specifying the related fields explicitly, you can perform a case-insensitive search using the 'icontains' lookup on the first_name and last_name fields of the Author model.

After making this change, you should be able to search for books based on the first name or last name of the author without encountering the FieldError related to the unsupported lookup.

0

The error you are encountering can be attributed to the utilization of the default search option in Django within your admin.py file.

Django's search option is designed to exclusively search for a field in the database, based on the column name rather than a table or row name.

To rectify this error, you have two potential solutions:

  1. Conduct a search within one or two columns of the User model:
from django.contrib import admin
from . import models

@admin.register(models.Post)
class PostModelAdmin(admin.ModelAdmin):
    ...
    search_fields = ('title', 'body', 'author__username')
    ...
  1. Alternatively, eliminate the author search functionality.
PicoDev
  • 57
  • 8