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

提问

观点 模型 错误 文件“C:\用户\ Atharva 您可以使用“Python \ Python39 \ lib \ \ lib \”来访问您的代码\ 啊。在build_filter的1339线中提高了FieldError(相关字段) 得到无效查找:{ } 。格式(查找[0])) django。core 。 exception。FieldError:相关字段无效查找: 标签 有人能回答吗 在你的视图中使用: 它意味着对象壁纸有属性标记和标签属性的值具有属性标记。还有标签。标签应该是django。model。Model的实例。 如果我看到类壁纸,我就不能找到任何相关的字段(m2m)到类标记(模型)。我只看到: 我认为这是壁纸的datamanager,而不是类标签的外移键。我的观点是,类名的复数形式是一个糟糕的要求。 尝试添加字段标记。类似这样的东西:

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)

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

回答

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

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

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

▼版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说cuoshuo.com——程序员的报错记录

部分内容根据CC版权协议转载,如果您希望取消转载请发送邮件到cuoshuo8@163.com

辽ICP备19011660号-5