gpt4 book ai didi

django - 精确查找的 QuerySet 值必须使用切片限制为一个结果。过滤器错误

转载 作者:行者123 更新时间:2023-12-03 15:22:43 24 4
gpt4 key购买 nike

我无法过滤相同的 slug 值。问题是我需要有两个相同的 slug 类型,我不明白如何修复它。我有两个带有 slug(种类)> 'silikonovyj-chehol' 的产品,我尝试过滤它,但是有这个 The QuerySet value for an exact lookup must be limited to one result using slicing.
View .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>]>

模型.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() 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)

如果你想根据 过滤一 种类,

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

如果要过滤 Products在所有 kind上面返回的对象。

关于django - 精确查找的 QuerySet 值必须使用切片限制为一个结果。过滤器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55994907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com