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:
DjangoFilterBackendOrderingFilterSearchFilter
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_relatedfor category and parent category.prefetch_relatedfor images, variants, wishlists, and reviews.- Annotates products with
avg_ratingusing an average of review ratings.
Filters:
- Color Filter
- Filters products by color variants.
-
Example:
?color=red,blue. -
Brand Filter
- Filters products by brand.
-
Example:
?brand=Nike,Adidas. -
Fabric Filter
- Filters products by fabric.
-
Example:
?fabric=cotton,polyester. -
Price Range Filter
- Filters products within a specified price range.
-
Example:
?min_price=100&max_price=500. -
Subcategory Filter
- Filters products by subcategories.
-
Example:
?subcategory=T-shirts. -
Category Filter
- Filters products by categories, including all descendants.
-
Example:
?category=men. -
Discount Range Filter
- Filters products by discount percentage ranges.
-
Example:
?discount_range=30. -
Rating Filter
- Filters products with ratings greater than or equal to a specified value.
-
Example:
?rating=4. -
Product Promotion Filter
- Filters products based on promotions.
- Example:
?product_promotion=20.
get_serializer_context()
Adds the request context to the serializer for user-related data fetching.
get()
Handles GET requests for:
- Search Suggestions
-
Example:
?query=shoes. -
Specific Product by ID or Slug
-
Example:
?id=123or?slug=running-shoes. -
Paginated Product List
- Applies filters and ordering.
Pagination:
- Uses
CustomPaginationto 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
- 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
ProductSerializershould handle related data serialization efficiently. - Use caching mechanisms for frequently accessed data to reduce database load.