Category Serializers Documentation
Overview
This document provides a comprehensive overview of the serializers used for products, variants, images, and categories in the e-commerce application. These serializers are essential for managing and presenting product and category data, including images, variants, and hierarchical relationships.
ProductImageSerializer
Purpose
Handles the serialization of product images.
Fields
- id: The primary key of the image.
- image: The URL of the image (absolute URL based on request context).
- variant: The associated product variant.
- is_primary: Indicates if the image is the primary image for the product.
- created_at: Timestamp when the image was created.
Methods
get_image
- Constructs the absolute URL of the image using the request context.
ProductVariantSerializer
Purpose
Handles the serialization of product variants, including associated images and sizes.
Fields
- id: The primary key of the variant.
- color: The color of the variant.
- value: Additional value information for the variant.
- variant_price: Price specific to the variant.
- stock_quantity: Available stock quantity.
- is_available: Indicates if the variant is available.
- is_active: Indicates if the variant is active.
- created_at: Timestamp when the variant was created.
- updated_at: Timestamp when the variant was last updated.
- product: The primary key of the associated product.
- size: Serialized size data.
- images: Serialized image data.
- primary_image: Primary image for the variant.
Methods
get_primary_image
- Fetches and serializes the primary image of the variant if available.
to_representation
- Ensures size information is always represented as an object.
validate_variant_price
- Ensures the variant price is greater than zero.
validate_stock_quantity
- Ensures the stock quantity is not negative.
create
- Handles the creation of a variant, associating size data if provided.
update
- Updates a variant, including handling size data.
delete
- Prevents deletion of variants with available stock.
ProductInCategorySerializer
Purpose
Handles the serialization of products within a category.
Fields
- id: The primary key of the product.
- name: The name of the product.
- slug: The URL-friendly identifier of the product.
- price: The price of the product.
- description: A detailed description of the product.
- is_featured: Indicates if the product is featured.
- is_active: Indicates if the product is active.
- images: Serialized product images.
- variants: Serialized product variants.
- created_at: Timestamp when the product was created.
- updated_at: Timestamp when the product was last updated.
ChildCategoryWithProductsSerializer
Purpose
Handles serialization for child categories, including their associated products.
Fields
- id: The primary key of the category.
- name: The name of the category.
- slug: The URL-friendly identifier of the category.
- description: A short description of the category.
- products: Serialized products within the category.
- banner_image: Banner image URL for the category.
- thumbnail_image: Thumbnail image URL for the category.
- created_at: Timestamp when the category was created.
- updated_at: Timestamp when the category was last updated.
Methods
get_products
- Fetches and serializes active products in the category.
get_banner_image
- Constructs the absolute URL of the banner image using the request context.
get_thumbnail_image
- Constructs the absolute URL of the thumbnail image using the request context.
ParentCategoryWithChildrenSerializer
Purpose
Handles serialization for parent categories, including their child categories and associated products.
Fields
- id: The primary key of the parent category.
- name: The name of the parent category.
- slug: The URL-friendly identifier of the parent category.
- description: A short description of the parent category.
- children: Serialized child categories with associated products.
- banner_image: Banner image URL for the parent category.
- thumbnail_image: Thumbnail image URL for the parent category.
- created_at: Timestamp when the parent category was created.
- updated_at: Timestamp when the parent category was last updated.
Methods
get_children
- Fetches and serializes child categories that have associated products.
get_banner_image
- Constructs the absolute URL of the banner image using the request context.
get_thumbnail_image
- Constructs the absolute URL of the thumbnail image using the request context.
Notes
- Ensure the request context is provided to the serializers for proper URL generation.
- Use the
ProductImageSerializerandProductVariantSerializerfor handling nested data effectively. - The
ChildCategoryWithProductsSerializerandParentCategoryWithChildrenSerializerensure hierarchical data representation while filtering for active products.