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 %}
<div>
<a href="{% url 'shop:product_show' product.slug product.id %}">{{ product.name }}</a>
<br>
{{ product.price }} ₽
<a href="{% url 'cart:cart_create' product.id %}"><button>Добавить в корзину</button></a>
</div>
{% 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,
Or:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With