Skip to content

Product List ViewSet Documentation

Overview

This documentation provides a comprehensive guide to the ProductListViewSet class, which is designed to handle product listings, filtering, searching, and pagination in an e-commerce API. The class supports advanced query parameters to refine product searches based on user preferences.


ProductListViewSet

  • ENDPOINT: /api/v1/catalog/product/list/

Inherits From:

ListAPIView

Permissions:

  • AllowAny: Allows unrestricted access to this view.

Serializer Class:

  • ProductSerializer

Pagination Class:

  • CustomPagination

Filter Backends:

  • DjangoFilterBackend
  • OrderingFilter
  • SearchFilter

Filterable Fields:

  • size: Filters products by size.

Ordering Fields:

  • price
  • popularity
  • avg_rating
  • created_at
  • discount_percent

Default Ordering:

  • Descending by avg_rating.

Search Fields:

  • name
  • description
  • slug
  • category.name
  • category.slug
  • category.description
  • category.parent.name
  • category.parent.slug
  • category.parent.description

Methods

get_queryset()

Generates a query set of active products with advanced filtering, annotation, and optimization.

Query Optimizations:

  • select_related for category and parent category.
  • prefetch_related for images, variants, wishlists, and reviews.
  • Annotates products with avg_rating using an average of review ratings.

Filters:

  1. Color Filter
  2. Filters products by color variants.
  3. Example: ?color=red,blue.

  4. Brand Filter

  5. Filters products by brand.
  6. Example: ?brand=Nike,Adidas.

  7. Fabric Filter

  8. Filters products by fabric.
  9. Example: ?fabric=cotton,polyester.

  10. Price Range Filter

  11. Filters products within a specified price range.
  12. Example: ?min_price=100&max_price=500.

  13. Subcategory Filter

  14. Filters products by subcategories.
  15. Example: ?subcategory=T-shirts.

  16. Category Filter

  17. Filters products by categories, including all descendants.
  18. Example: ?category=men.

  19. Discount Range Filter

  20. Filters products by discount percentage ranges.
  21. Example: ?discount_range=30.

  22. Rating Filter

  23. Filters products with ratings greater than or equal to a specified value.
  24. Example: ?rating=4.

  25. Product Promotion Filter

  26. Filters products based on promotions.
  27. Example: ?product_promotion=20.

get_serializer_context()

Adds the request context to the serializer for user-related data fetching.


get()

Handles GET requests for:

  1. Search Suggestions
  2. Example: ?query=shoes.

  3. Specific Product by ID or Slug

  4. Example: ?id=123 or ?slug=running-shoes.

  5. Paginated Product List

  6. Applies filters and ordering.

Pagination:

  • Uses CustomPagination to paginate the results.

Usage Examples

Basic Usage

  • Get all products: GET /api/v1/catalog/product/list/

Filters

  • Get products by category: GET /api/v1/catalog/product/list/?category=men
  • Get products within a price range: GET /api/v1/catalog/product/list/?min_price=100&max_price=500
  • Get products with a discount: GET /api/v1/catalog/product/list/?discount_range=20
  • Search for products: GET /api/v1/catalog/product/list/?search=shoes

Ordering

  • Order by price (ascending): GET /api/v1/catalog/product/list/?ordering=price
  • Order by rating (descending): GET /api/v1/catalog/product/list/?ordering=-avg_rating

Notes

  • Ensure database indexing for fields used in filtering and ordering to optimize performance.
  • The ProductSerializer should handle related data serialization efficiently.
  • Use caching mechanisms for frequently accessed data to reduce database load.