Добрый день. Подскажите пожалуйста. Я хочу сделать выборку продуктов по категориям. Категории лежат в другой таблице.
Model категории:class Category(models.Model):
name = models.CharField(max_length = 100)
slug = models.SlugField()
def __str__(self):
return self.name
Model Продуктов:class Product(models.Model):
articul = models.CharField(max_length = 120)
category = models.ForeignKey(
'Category',
on_delete=models.CASCADE
)
name = models.CharField(max_length = 120)
slug = models.SlugField()
description = models.TextField()
composition = models.TextField()
color = models.ForeignKey(
'Color',
on_delete=models.CASCADE
)
brand = models.ForeignKey(
'Brand',
on_delete=models.CASCADE
)
picturePrint = models.ForeignKey(
'PicturePrint',
on_delete=models.CASCADE
)
pictures = models.ImageField(upload_to='shop/%Y/%m/%d', blank=True)
price = models.DecimalField(max_digits = 9, decimal_places = 2)
date = models.DateTimeField(auto_now = True)
discount = models.PositiveSmallIntegerField(default=0)
available = models.BooleanField(default = True)
def __str__(self):
return self.name
Это url:urlpatterns = [
path('', views.shop, name='shop'),
path('<slug>', views.categoriesProduct, name='categoriesProduct'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
И собственно когда я пытаюсь сделать выборку:
def categoriesProduct(request, slug):
category = Category.objects.get(slug = slug)
productsList = Product.objects.filter(category=category)
return HttpResponse(productsList)
У меня все отрабатывает. Но в консоль вот такая ошибка:
[23/Sep/2018 13:42:55] "GET /furniture-fabrics HTTP/1.1" 200 353
Internal Server Error: /favicon.ico
Traceback (most recent call last):
File "/var/www/env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/var/www/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/var/www/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/tkaneva/shop/views.py", line 20, in categoriesProduct
category = Category.objects.get(slug = slug)
File "/var/www/env/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/var/www/env/lib/python3.5/site-packages/django/db/models/query.py", line 403, in get
self.model._meta.object_name
shop.models.DoesNotExist: Category matching query does not exist.