How to fetch those products that are belongs to the Offer class and offer is belongs to Product_tab from template in django?
This is my views.py
def offers(request):
pro_tab = Product_tab.objects.all()
for i in pro_tab:
for j in i.offer.all():
for k in j.product:
print(k.name)
context = {
'pro_tab':pro_tab,
}
return render(request, 'pages/offers.html', context)
This is my models.py
class Offer(models.Model):
offer_name = models.CharField(max_length=100)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.offer_name
class Product_tab(models.Model):
prod_tab = models.CharField(max_length=100)
offer = models.ManyToManyField(Offer)
def __str__(self):
return self.prod_tab
class Product(models.Model):
name = models.CharField(max_length=150)
is_stock = models.BooleanField(default=True)
offer = models.ForeignKey(Offer, on_delete=models.CASCADE)
product_for = models.ForeignKey(Product_for, on_delete=models.CASCADE)
product_tab = models.ForeignKey(Product_tab, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.name
This is my html page
<section class="offer-section">
<div class="row mx-5 mt-5">
<div class="gallery-filter mb-5">
<ul class="list-unstyled">
<li class="active" data-filter="*">All</li>
{% for i in pro_tab %}
<li data-filter=".{{ i|lower }}">{{ i|capfirst }}</li>
{% endfor %}
</ul>
</div>
<div class="gallery mt-4 mb-5 text-white">
{% for i in pro_tab %}
{% for j in i.offer.all %}
<div class="col-lg-12 col-md-12 isotope-item {{ i|lower }}">
<div class="active-offer-title py-3 mb-3">
<h3 class="pl-3">{{ j.offer_name }} offer</h3>
</div>
<div class="row">
{% for k in i.offer.product %}
<div class="col-md-3 col-6 mb-3">
<a href="food-details.html" target="_blank" class="text-decoration-none">
<div class="card">
<div class="card-img">
<img src="{% static 'img/offer/o-1.jpeg' %}" class="img-fluid" alt="">
</div>
<div class="card-content">
<div class="card-title">
<h5 class="text-center mt-2 mb-0">{{ k.name }}</h5>
In my HTML page, all Product_tab are displayed, all offers are displayed but products that are belonged to the offer not showing.
In my Product class Offer is ForeignKey and In my Product_tab Offer is ManyToManyField.