M
M
Mikhail Yarema2018-09-23 13:55:34
Django
Mikhail Yarema, 2018-09-23 13:55:34

How to make a selection by ForeignKey in Django ORM?

Good afternoon. Tell me please. I want to select products by category. The categories are in another table.
Model category:

class Category(models.Model):

    name = models.CharField(max_length = 100)
    slug = models.SlugField()

    def __str__(self):
        return self.name

Product 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

This is the url:
urlpatterns = [
    path('', views.shop, name='shop'),
    path('<slug>', views.categoriesProduct, name='categoriesProduct'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

And actually when I try to make a selection:
def categoriesProduct(request, slug):
    category = Category.objects.get(slug = slug)
    productsList = Product.objects.filter(category=category)
    return HttpResponse(productsList)

Everything works for me. But here is the error in the console:
[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.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question