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

huangapple go评论65阅读模式
英文:

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

问题

我正在尝试在我的Books模型中搜索Author。

英文:

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.

答案1

得分: 1

你遇到的错误可以归因于在你的 admin.py 文件中使用了 Django 中的默认搜索选项。

Django 的搜索选项是设计用来仅在数据库中搜索字段的,基于列名而不是表名或行名。

要纠正这个错误,你有两个潜在的解决方案:

  1. 在 User 模型的一个或两个列中进行搜索:
from django.contrib import admin
from . import models

@admin.register(models.Post)
class PostModelAdmin(admin.ModelAdmin):
    ...
    search_fields = ('title', 'body', 'author__username')
    ...
  1. 或者,取消作者搜索功能。
英文:

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.

答案2

得分: 0

为解决此问题,您可以修改 search_fields 属性以在 Author 模型的相关字段上进行搜索。以下是我 BookAdmin 类的更新版本:

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',),
    }

在修改后的 search_fields 中,'author__first_name' 和 'author__last_name' 用于在相关的 Author 模型的 first_name 和 last_name 字段上进行搜索。__ 语法用于遍历 Book 和 Author 模型之间的关系。

通过明确指定相关字段,您可以在 Author 模型的 first_name 和 last_name 字段上执行不区分大小写的搜索,使用 'icontains' 查找。

在进行此更改后,您应该能够根据作者的名字或姓氏搜索图书,而不会遇到与不支持的查找相关的 FieldError。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月5日 20:42:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定