Search code examples
djangodjango-viewsdjango-templatesdjango-generic-views

Django The QuerySet value for an exact lookup must be limited to one result using slicing Error


I have this problem when I want to show all of the post that related to category. I couldn't find the solution in other similar questions. Thank you. Error: The QuerySet value for an exact lookup must be limited to one result using slicing.

Model:

class Category(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField()

class Post(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)
    body = models.TextField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, null=True)

    def get_absolute_url(self):
        return reverse("post", kwargs={'slug': self.slug})

View :

class PostListView(ListView):
"""
Return all of the posts
"""
model = Post
ordering = '-created' 
context_object_name = 'posts'
template_name ='blog/post_list.html'

def get_queryset(self):
    qs = Post.objects.all()
    if self.kwargs.get('slug'):
        category_slug = self.kwargs.get('slug')
        qs = Post.objects.filter(category = Category.objects.filter(slug = category_slug))
    return qs

URL:

path('', PostListView.as_view(), name='posts'), # all of the posts
path('category/<slug:slug>/', PostListView.as_view(), name='categoryposts'),

Template:

    <div class="row">
        {% for post in posts %}
            <div class="col-md-4">
                <div class="card" style="width: 25rem; height: 34rem;">
                    <a href="{% url 'post' post.slug %}">
                        <img class="post-thumbnail card-img-top" src="{{post.thumbnail.url}}" alt="{{post.title}}" 
                        style="width: 24.9rem; height: 18rem;">
                    </a>
                    <div class="card-body">
                        <h5 class="card-title"><a href="{% url 'post' post.slug %}">{{post.title}}</a></h5>
                        <p class="post-updated">Last update at {{post.updated|date:"Y/M/d"}}</p>
                        <p>Post by <a href="">{{post.author}}</a></p>
                        {% if post.description %}
                        <p class="card-text">{{post.description|slice:"80"}}...</p>
                        {% else %}
                        <p class="card-text">{{post.body|slice:"80"}}...</p>
                        {% endif %}
                        <a href="{% url 'post' post.slug %}" class="btn btn-primary" style="position: absolute; bottom: 0.5rem;">Read</a>
                    </div>
                </div>
                <br>
            </div>
        {% endfor %}
    </div>

Solution

  • I've found my answer. if you have the same issue in the qs line write this :

    category_slug = self.kwargs.get('slug')
    category = Category.objects.get(slug = category_slug)
    category_id = Category.objects.get(id = category.id)
    qs = Post.objects.filter(category = category_id)
    

    instead of this:

    category_slug = self.kwargs.get('slug')
    qs = Post.objects.filter(category = Category.objects.filter(slug = category_slug))