Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Django taggit view leading to FieldError: Related Field got invalid lookup: name

$
0
0

Tag view leads to this error message.The FieldError: Related Field got invalid lookup: name indicates a mismatch between what the Django ORM expects for a query on the tags relation and the actual query being executed.

[...] File "/workspace/Coach-Matrix/main_forum/views/filters.py", line 18, in get_queryset    return Question.objects.filter(tags__name=tag.name)  File "/workspace/.pip-modules/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method    return getattr(self.get_queryset(), name)(*args, **kwargs)  File "/workspace/.pip-modules/lib/python3.9/site-packages/django/db/models/query.py", line 1420, in filter    return self._filter_or_exclude(False, args, kwargs)  File "/workspace/.pip-modules/lib/python3.9/site-packages/django/db/models/query.py", line 1438, in _filter_or_exclude    clone._filter_or_exclude_inplace(negate, args, kwargs)  File "/workspace/.pip-modules/lib/python3.9/site-packages/django/db/models/query.py", line 1445, in _filter_or_exclude_inplace    self._query.add_q(Q(*args, **kwargs))  File "/workspace/.pip-modules/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1532, in add_q    clause, _ = self._add_q(q_object, self.used_aliases)  File "/workspace/.pip-modules/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1562, in _add_q    child_clause, needed_inner = self.build_filter(  File "/workspace/.pip-modules/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1478, in build_filter    condition = self.build_lookup(lookups, col, value)  File "/workspace/.pip-modules/lib/python3.9/site-packages/django/db/models/sql/query.py", line 1292, in build_lookup    raise FieldError(Exception Type: FieldError at /questions/tag/tag4/Exception Value: Related Field got invalid lookup: name

I had tried Fetching the Tag instance using its slug and Using the name of the fetched Tag instance to filter Question objects.

from django.views.generic.list import ListViewfrom ..models import Questionfrom django.shortcuts import get_object_or_404from taggit.models import Tagclass FilterByTagView(ListView):    model = Question    template_name = 'filtered_questions.html'    context_object_name = 'questions'    def get_queryset(self):        tag_slug = self.kwargs.get('tag_slug')        # Retrieve the Tag object by slug        tag = get_object_or_404(Tag, slug=tag_slug)        # Use TaggedItemManager's filter method to get questions tagged with the retrieved tag        return Question.objects.filter(tags__slug=tag.slug)    def get_context_data(self, **kwargs):        context = super().get_context_data(**kwargs)        tag_slug = self.kwargs.get('tag_slug')        context['tag'] = get_object_or_404(Tag, slug=tag_slug)        return context

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>