django 相关字段的查找无效:中的内容admin.py

xpcnnkqh  于 2023-02-17  发布在  Go
关注(0)|答案(1)|浏览(99)

我知道这个问题已经张贴之前,其各自的解决方案,但我不知道是什么问题,在这个特定的情况下,我试图添加一个搜索栏在管理页面的一个特定的类称为"菜"这里是上下文:

    • 浏览器出错**
FieldError at /admin/menu/dish/
Related Field got invalid lookup: icontains
Request Method: GET
Request URL:    http://www.lupita.restaurant/admin/menu/dish/?q=naranja
Django Version: 4.0.6
Exception Type: FieldError
Exception Value:    
Related Field got invalid lookup: icontains
Exception Location: /usr/local/lib/python3.10/site-packages/django/db/models/sql/query.py, line 1262, in build_lookup
Python Executable:  /usr/local/bin/uwsgi
Python Version: 3.10.5
Python Path:    
['/home/juanfrax/lupita',
 '/var/www',
 '.',
 '',
 '/var/www',
 '/usr/local/lib/python310.zip',
 '/usr/local/lib/python3.10',
 '/usr/local/lib/python3.10/lib-dynload',
 '/home/juanfrax/.local/lib/python3.10/site-packages',
 '/usr/local/lib/python3.10/site-packages']
Server time:    Fri, 10 Feb 2023 03:38:57 +0000
Traceback Switch to copy-and-paste view

/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py, line 55, in inner
                response = get_response(request) …
Local vars
/usr/local/lib/python3.10/site-packages/django/core/handlers/base.py, line 197, in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
Local vars
/usr/local/lib/python3.10/site-packages/django/contrib/admin/options.py, line 683, in wrapper
                return self.admin_site.admin_view(view)(*args, **kwargs) …
Local vars
/usr/local/lib/python3.10/site-packages/django/utils/decorators.py, line 133, in _wrapped_view
                    response = view_func(request, *args, **kwargs) …
Local vars
/usr/local/lib/python3.10/site-packages/django/views/decorators/cache.py, line 62, in _wrapped_view_func
        response = view_func(request, *args, **kwargs) …
Local vars
    • 型号. py:**
class Dish(models.Model):
    name = models.CharField(max_length=50)
    nombre = models.CharField(max_length=50)
    description = models.TextField(max_length=500)
    descripcion = models.TextField(max_length=500)
    category = models.ForeignKey('Category', null=True, on_delete=models.SET_NULL)
    categoria = models.ForeignKey('Categoria', null=True, on_delete=models.SET_NULL)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    image = models.ImageField(upload_to='dishes_img/', blank=True, null=True)
    tags = TaggableManager(blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'dish'
        verbose_name_plural = 'dishes'

class Category(models.Model):
    category_name = models.CharField(max_length=50)
    slug = models.SlugField(blank=True, null=True)

    class Meta:
        verbose_name = ' category'
        verbose_name_plural = 'categories'

    def __str__(self):
        return self.category_name

    def save(self, *args, **kwargs):
        if not self.slug and self.category_name:
            self.slug = slugify(self.category_name)
        super(Category, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('menu:view_menu_slug_en', args=[self.slug])

class Categoria(models.Model):
    nombre_categoria = models.CharField(max_length=50)
    slug = models.SlugField(blank=True, null=True)

    def __str__(self):
        return self.nombre_categoria

    def save(self, *args, **kwargs):
        if not self.slug and self.nombre_categoria:
            self.slug = slugify(self.nombre_categoria)
        super(Categoria, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('menu:view_menu_slug_es', args=[self.slug])
    • 管理员py**
from django.contrib import admin
from django.contrib.admin import AdminSite

from .models import Dish, Categoria

# Register your models here.

class MealsAdmin(admin.ModelAdmin):  # instead of ModelAdmin
    list_display = ['nombre', 'descripcion', 'categoria', 'price']
    search_fields = ['nombre', 'descripcion']
    list_filter = ['categoria']

admin.site.register(Dish, MealsAdmin)
admin.site.register(Categoria)
AdminSite.enable_nav_sidebar = False

我想查询的结果,这是很明显的,谢谢你的意见。

gab6jxml

gab6jxml1#

您还应在www.example.com中MealsAdminsearch_fields属性中添加name字段admin.py以便:

class MealsAdmin(admin.ModelAdmin):  # instead of ModelAdmin
    list_display = ['nombre', 'descripcion', 'categoria', 'price']
    search_fields = ['nombre', 'descripcion', 'name']
    list_filter = ['categoria']

这将允许您搜索菜肴的名称或描述,而不考虑大小写。

相关问题