Code Of Models
class Product(models.Model):
title = models.CharField(max_length=255)
category = models.CharField(max_length=255, blank=True)
availability = models.CharField(max_length=50)
warranty = models.IntegerField()
display_description = models.TextField()
price = models.IntegerField()
original_price = models.IntegerField()
product_description = models.TextField()
first_image = models.ImageField(upload_to='shop/images', default="")
is_active = models.BooleanField(default=True)
parent = models.ForeignKey(
"self", on_delete=models.CASCADE, related_name="parent_category", null=True, blank=True)
def __str__(self):
if self.parent and self.category:
return f"{self.parent}>{self.category}"
elif self.category:
return self.category
else:
return ""```
Code Of Views
class IphoneSe(ListView):
template_name = ‘iphonese.html’
model = Product
context_object_name = 'iphone_listings'
def get_queryset(self):
# Define the 'parent' parameter you want to filter by
parent_param = 'Apple>IPhone' # You can change this to any value you want
# Debugging: Print the 'parent_param' to check if it's correctly set
print("Parent Parameter:", parent_param)
# Filter products based on the selected 'parent' parameter
queryset = self.model.objects.filter(parent__title=parent_param)
# Debugging: Print the resulting queryset to check its contents
print("Filtered QuerySet:", queryset)
return queryset
When I Hit The Url Then On terminal Showing This
Parent Parameter: Apple>IPhone
Filtered QuerySet: <QuerySet >
[05/Oct/2023 09:32:56] “GET /iphonese/ HTTP/1.1” 200 36416
Not Found: /iphonese/assets/img/apple-cover.jpg
[05/Oct/2023 09:32:56] “GET /iphonese/assets/img/apple-cover.jpg HTTP/1.1” 404 8039