I have 2 models, 1 for Product and 1 for ProductImages with pagination set to 10. For any product, we can have associated multiple images.
class Product(models.Model):
title = models.CharField(max_length=200)
details = models.TextField(null = True)
slug = models.CharField(max_length=300, unique=True, null=True)
class ProductImages(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='product_imgs')
image = models.ImageField(upload_to='product_imgs/', null=True)
def __str__(self):
return self.image.url
My ProductList view looks like,
class ProductList(generics.ListCreateAPIView):
queryset = models.Product.objects.all()
serializer_class = serializers.ProductListSerializer
pagination_class = pagination.PageNumberPagination
def get_queryset(self):
qs = super().get_queryset()
if self.request.method == 'GET':
if 'category' in self.request.GET:
category = self.request.GET['category']
category = models.ProductCategory.objects.get(id=category)
qs = qs.filter(category=category)
if 'owner' in self.request.GET:
print('get_queryset Owner')
owner = self.request.GET['owner']
qs = qs.filter(owner=owner)
if 'available' in self.request.GET:
available = self.request.GET['available'] == 'true'
qs = qs.filter(available=available)
ids = []
for product in qs:
# images = models.ProductImages.objects.filter(id=product.id)
print('Images: %d', product.id)
ids.append(product.id)
for id in ids:
print('Id %d', id)
images = models.ProductImages.objects.filter(product__in=ids)
I am trying to fetch products and images associated with those products. When I fetch products I get 10 products (as per set pagination) and I get 10 images. But problem is I want to get all (say) 25 images associated with those 10 products, i.e. I want to turn off the pagination only for filter on ProductImages model. How can I achieve this?
I have tried looking up getting only one image per product but I couldn't find a solution for that either.