Answer the question
In order to leave comments, you need to log in
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
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
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
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question