本文介绍了精确查找的QuerySet值必须使用切片限制为一个结果.过滤错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法过滤相同的段塞值.问题是我需要使用两种相同的弹头,但我不知道如何解决.我有两种产品,它们带有"slug(kind)">"silikonovyj-chehol",并且尝试对其进行过滤,但是具有此用于精确查找的QuerySet值必须限制为使用切片的一个结果.

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 %}

下面

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

我在 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)

或者,如果我更改此行,则我有 get()返回了不止一种-它返回了2!错误

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)

推荐答案

此:

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

应该是:

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)

如果要过滤上面返回的所有种类对象上的 Products .

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

这篇关于精确查找的QuerySet值必须使用切片限制为一个结果.过滤错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:35