django 当我尝试搜索时,我得到这个错误:相关字段的查找无效:标签

laik7k3q  于 2023-04-22  发布在  Go
关注(0)|答案(2)|浏览(99)

views.py

def home(request):
WAllPAPER_PER_PAGE = 15
WALL = Wallpaper.objects.all()
from django.core.paginator import EmptyPage, Paginator
from django.db.models import Q
qd = request.GET.copy()
qd.pop('page', None)
querystring = qd.urlencode()

#link formatting for ordering
ordering =request.GET.get('ordering', "")

#link formatting for sorting
search = request.GET.get('search', "")
if search:
    wallpapers = Wallpaper.objects.filter(Q(name__icontains=search) | Q(category__category_name__icontains=search) | Q(tags__tag__icontains=search)).distinct().order_by('-pk')
    WALL = None
else:
    wallpapers = Wallpaper.objects.all().order_by('-pk')
if ordering:
    wallpapers = wallpapers.order_by(ordering)
page = request.GET.get('page', 1)
wallpaper_paginator = Paginator(wallpapers, WAllPAPER_PER_PAGE)
try:
    wallpapers = wallpaper_paginator.page(page)
except EmptyPage:
    wallpapers = wallpaper_paginator.page(wallpaper_paginator.num_pages)
except:
    wallpapers = wallpaper_paginator.page(WAllPAPER_PER_PAGE)
context = {'querystring': querystring, "wallpapers": wallpapers, 'page_obj': wallpapers, 'is_paginated': True, 'paginator': wallpaper_paginator, 'WALL': WALL}
return render(request, "Wallpaper/Home.html", context)

models.py

class Tags(models.Model):
tag = models.CharField(max_length=100)

def __str__(self):
    return self.tag

class Category(models.Model):
    category_name = models.CharField(max_length=100)

    def __str__(self):
        return self.category_name

class Wallpaper(models.Model):
    name = models.CharField(max_length=100, null=True)
    size = models.CharField(max_length=50, null=True)
    pub_date = models.DateField('date published', null=True)
    resolution = models.CharField(max_length=100, null=True)
    category = models.ManyToManyField(Category)
    tags = TaggableManager()
    Device_Choices = [
        ('PC', 'pc'),
        ('mobile', 'mobile')
    ]
    Devices = models.CharField(max_length=20,choices=Device_Choices, default= 'PC')
    image = models.ImageField(upload_to='Wallpaper/Images/', default="")

    def __str__(self):
        return self.name

误差
文件“C:\Users\Atharva thaware\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\query.py“,第1339行,在build_filter中raise FieldError('Related Field got invalid lookup:{}'. format(lookups[0]))django.core.exceptions.FieldError:相关字段的查找无效:标签
谁能回答一下

z9zf31ra

z9zf31ra1#

在视图home中,您用途:

Wallpaper.objects.filter(Q(tags__tag__icontains=search))

这意味着,对象wallpaper具有属性tags,并且tags属性的值具有属性tag。此外,tags.tag应该是django.model.Model的instance。
如果我看到class Wallpaper,我找不到任何相关字段(o2 m,m2m)到class tags(Model)。我只看到:

class Wallpaper(models.Model):
    ...
    tags = TaggableManager()

我认为这是墙纸的datamanager,而不是class Tags的外键。我的意见-类名的复数形式是一个糟糕的设计。
尝试添加字段标签。类似于:

class Wallpaper(models.Model):
    ...
    tags = models.ManyToManyField(Tags)
    tagged = TaggableManager()
cmssoen2

cmssoen22#

也许你用的不是最新版本的django-taggit,我花了一个早上的时间来处理这个错误,只是为了意识到更新到django-taggit 3.1.0,这个问题就解决了。

相关问题