Mario_z04
Mario_z04

Reputation: 51

when I try to search I get this error : Related Field got invalid lookup: tag

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

error

File "C:\Users\Atharva thaware\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\q uery.py", line 1339, in build_filter raise FieldError('Related Field got invalid lookup: {}'.format(lookups[0])) django.core.exceptions.FieldError: Related Field got invalid lookup: tag

can someone please answer

Upvotes: -1

Views: 57

Answers (2)

Maberi
Maberi

Reputation: 11

Maybe you are not using the last version of django-taggit. I've spent all the morning with this error just to realize that updating to django-taggit 3.1.0, the problem was solved.

Upvotes: 0

Maxim Danilov
Maxim Danilov

Reputation: 3350

in your view home you use:

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

it means, object wallpaper has attribute tags, and value of tags attribute has attribute tag. moreover tags.tag should be an instance of django.model.Model.

And if i see class Wallpaper, i can not find any relatedfield (o2m, m2m) to class tags(Model). I see only:

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

i think this is datamanager for wallpaper, and not the foreignkey to class Tags. My opinion - the Plural form for class name is a bad desigion.

Try to add field tags. Something like that:

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

Upvotes: 1

Related Questions