K
K
Kamral Magaramov2021-01-25 23:59:57
Django
Kamral Magaramov, 2021-01-25 23:59:57

Why does it return an empty queryset instead of records?

from django.urls import path
from .views *

urlpaterns=[
    path('product/<int:pk>/', product_detail, name='product_detail'),
]


#urls.py


#views.py
def product_detail(request,pk):
    products=product.objects.filter(pk=pk)
    title_spisok=spisok.objects.filter(product=pk)
    punkt=punkt_of_spisok.objects.filter(spisok=pk)
    return render(request,'product_detail.html', {'products':products,
                                                  'title_product':title_spisok,
                                                  'punkts':punkt
                                                  })
#models.py
class punkt_of_spisok(models.Model):
    punkt=models.CharField(max_length=255, verbose_name='Пукт')
    spisok=models.ForeignKey('spisok',on_delete=models.CASCADE)

class spisok(models.Model):
    title=models.CharField(max_length=255, verbose_name='Заголовок списка')
    product=models.ForeignKey('product', verbose_name='Продукт', on_delete=models.CASCADE)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kamral Magaramov, 2021-01-26
@Kamral

your decision was wrong.
it would be more correct to write:
punkt=punkt_of_spisok.objects.filter(spisok__product=pk) -
we indicate here that we are filtering the punkt_of_spisok data by the external key product of the spisok model, and taking all the records. As a result, we get items related to a specific list of headings, which in turn belong to a specific product. That is, we have formed a full-fledged relationship

A
Anton Belokurov, 2021-01-26
@kyern

Which of the three querysets is empty?
And are you sure it's empty? You don't pass context variable to render https://docs.djangoproject.com/en/3.1/topics/http/...
Should be

return render(request,'product_detail.html', context={'products':products,
                                                  'title_product':title_spisok,
                                                  'punkts':punkt
                                                  })

PS Once again I looked at the documentation, where context is also passed as a positional argument, not a named one.
But in general, you are punkt_of_spisok by the primary key of the product, not the list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question