相关文章推荐
完美的排球  ·  c++ - gdipluspath ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error

Ask Question

I can not filter the same slug values. The problem is that I need to have two identical slug kind and I don't understand how fix it. I have two product with slug (kind) > 'silikonovyj-chehol' and I try filtering it, but have this The QuerySet value for an exact lookup must be limited to one result using slicing.

views.py

def product_list(request, category=None, subcategory=None, kind=None):
    if category:
        category = Category.objects.get(slug=category)
        categories = Category.objects.all()
        subcategories = Subcategory.objects.filter(category=category)
        products = Product.objects.filter(category=category, available=True)
        kinds = None
        if subcategory:
            subcategory = Subcategory.objects.get(slug=subcategory)
            kinds = Kind.objects.filter(kind=subcategory)
            products = Product.objects.filter(category=category, subcategory=subcategory, available=True)
            if kind:
                kind = Kind.objects.filter(slug=kind) # ERROR IT'S HERE
                products = Product.objects.filter(category=category, subcategory=subcategory, kind=kind, available=True)
        context = {
            'categories':categories,
            'category':category,
            'subcategories':subcategories,
            'subcategory':subcategory,
            'products':products,
            'kinds':kinds,
        return render(request, 'shop/product/product_list.html', context)
    else:
        categories = Category.objects.all()
        products = Product.objects.filter(available=True)
        context = {'categories':categories, 'products':products}
        return render(request, 'shop/product/product_list.html', context)

product_list.html

{% if subcategory %}
    {% for kind in kinds %}
        <a href="{% url 'shop:lst_by_knds' category.slug subcategory.slug kind.slug %}">{{ kind.name }}</a>
    {% endfor %}
{% endif %}

Below

{% for product in products %}
        <a href="{% url 'shop:product_show' product.slug product.id %}">{{ product.name }}</a>
        {{ product.price }} &#8381;
        <a href="{% url 'cart:cart_create' product.id %}"><button>Добавить в корзину</button></a>
{% endfor %}

I try it in python manage.py shell

>> kind = 'silikonovyj-chehol'
>> Kind.objects.filter(slug=kind)
>> <QuerySet [<Kind: Silicone Case>, <Kind: Silicone Case>]>

models.py

class Category(models.Model):
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True)
class Subcategory(models.Model):
    category = models.ForeignKey(Category, on_delete=models.PROTECT)
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True)
class Kind(models.Model):
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True)
    kind = models.ForeignKey(Subcategory, on_delete=models.PROTECT)
class Product(models.Model):
    category = models.ForeignKey(Category, on_delete=models.PROTECT)
    subcategory = models.ForeignKey(Subcategory, on_delete=models.PROTECT)
    kind = models.ForeignKey(Kind, on_delete=models.PROTECT)
    name = models.CharField(max_length=200, db_index=True)

Or if I change this line, I have get() returned more than one Kind -- it returned 2! Error

if kind:
   kind = Kind.objects.get(slug=kind)

This:

products = Product.objects.filter(category=category, subcategory=subcategory, kind=kind, available=True)

Should be either:

products = Product.objects.filter(category=category, subcategory=subcategory, kind=kind[0], available=True)

if you want to filter based on one kind,

products = Product.objects.filter(category=category, subcategory=subcategory, kind__in=kind, available=True)

if you want to filter Products on all kind objects returned above.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.