taggit django error, access posts through the links of the tags, error Cannot query "jazz": Must be "Post" instance

102 Views Asked by At

I'm new to Django and I'm studying through the book Django 3 by example, when implementing the tags, at the end of chapter 2, I can't access them. The assigned error is as follows:

"Cannot query "jazz": Must be "Post" instance"

my views.py

def post_list(request, tag_slug=None):
    object_list = Post.published.all()
    tag = None
    if tag_slug:
        tag = get_object_or_404(Tag, slug=tag_slug)
        object_list = object_list.filter(tags__in=[tag])

    paginator = Paginator(object_list, 2) # 2 posts in each page
    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:     
        # If page is not an integer deliver the first page
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)

    return render(request,'blog/post/list.html',{'page': page,'posts': posts, 'tag': tag})

my urls.py

urlpatterns = [
    # post views
    path('', views.post_list, name='post_list'),
    path('tag/<slug:tag_slug>/', views.post_list, name='post_list_by_tag'),
    # path('', views.PostListView.as_view(), name='post_list'),
    path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail'),
    path('<int:post_id>/share/', views.post_share, name='post_share'),

my list.html

% block content %}
  <h1>My Blog</h1>
  {% if tag %}
    <h2>Posts tagged with "{{ tag.name }}"</h2>
  {% endif %}
  {% for post in posts %}
    <h2>
      <a href="{{ post.get_absolute_url }}">
        {{ post.title }}
      </a>
    </h2>
    <p class="tags">
      Tags:
      {% for tag in post.tags.all %}
        <a href="{% url "blog:post_list_by_tag" tag.slug %}">
          {{ tag.name }}
        </a>
        {% if not forloop.last %}, {% endif %}
      {% endfor %}
    </p>
    <p class="date">
      Published {{ post.publish }} by {{ post.author }}
    </p>
    {{ post.body|truncatewords:30|linebreaks }}
  {% endfor %}
  {% include "pagination.html" with page=posts %}
{% endblock %}

Please any solution about it? I greatly appreciate it.

I have tried to rewrite the views, as well as examine and replace the labels and tag names, without success.

1

There are 1 best solutions below

0
Jake On

This appears to be a bug that has been resolved in a newer version of django-taggit than the one used in the Django 3 by Example, Third Edition book. For example, see the related self-answered question from @Cario Cogni.

At the time the book was written, django-taggit==1.2.0 was noted as the install path, as that was the most up-to-date version of the library. The current version is django-taggit==3.1.0, so there have been considerable changes in the past several years.

Additionally, the filtering itself has apparently changed. In the book, it is done like this:

Post.objects.filter(tags__in=[...])

However, the documentation for django-taggit now filters slightly differently:

Post.objects.filter(tags__name__in=[...])

I reproduced the issue by replicating the code as it was provided in the book, and was able to fix the issue by following these steps:

  1. Update the django-taggit library by running pip install django-taggit==3.1.0 I didn't need to force a reinstall, as was done in the related question
  2. Update the filter code as noted above, using tags__name__in instead of simply tags__in for the keyword arg.