CategoryListViewSet Documentation
Overview
CategoryListViewSet is a Django REST Framework viewset designed to retrieve and filter categories. It handles both parent categories and child categories, with filtering options available based on their IDs, slugs, and other attributes. Additionally, it supports pagination and search functionality.
ENDPOINT
- ENDPOINT:
api/v1/catalog/category/list/ - permission:
AllowAny - METHOD:
GET
Methods: get(self, request, *args, **kwargs)
Handles the GET request to retrieve categories. It processes multiple filtering scenarios: filtering by parent category, child category, or returning the full list.
Parameters:
- request: The HTTP request.
- args: Positional arguments for the view.
- kwargs: Keyword arguments for the view.
Logic:
- Parent Category Filtering:
- If
parent_category__idorparent_category__slugis provided in the query parameters, it filters categories by the providedidorslug. -
If a valid category is found, it returns the category data using
ParentCategoryWithChildrenSerializer. -
Child Category Filtering:
- If
idorslugfor a child category is provided, the view filters categories with a parent (i.e., subcategories). - Additionally, it annotates the results with a
product_countto only include subcategories that have products (product_count__gt=0). -
If a valid category is found, it returns the category data using
ChildCategoryWithParentSerializer. -
Default Parent Category List:
- If no filters are applied, it returns the default parent categories with pagination.
Response:
- HTTP 200 OK: Returns the filtered categories in JSON format.
- HTTP 204 No Content: Returns the filtered categories in JSON format.
get_queryset(self)
Overrides the default get_queryset method to allow additional filtering for subcategories that contain products. It supports a subcategory_search query parameter, which filters categories by name and ensures that only subcategories with products are included.
Parameters
None
Logic:
- If
subcategory_searchquery parameter is provided, it filters categories withparent__isnull=Falseandname__icontains=subcategory_search. - Additionally, it annotates the categories with a
product_countand filters those with aproduct_countgreater than 0.
Response:
- Returns the filtered queryset of categories.
Filtering & Search
Parent Category Filtering:
- Use
parent_category__idorparent_category__slugin query parameters to filter by parent category ID or slug.
Child Category Filtering:
- Use
idorslugto filter by child category ID or slug. Only subcategories with products will be returned.
Search:
- Use the
searchparameter to search categories by fields such asname,description,slug, and parent category details.
Example Requests
{
"pageSize": 20,
"count": 3,
"page_count": 1,
"active_page": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"name": "Kurtas",
"slug": "kurtas",
"description": "This is the Kurtas category",
"children": [
{
"id": 4,
"name": "Kids Kurtas",
"slug": "kids-kurtas",
"description": "This is the Kids Kurtas subcategory of Kurtas",
"products": [
{
"id": 21,
"name": "Late Kurtas",
"slug": "late-kurtas",
"price": "519.00",
"description": "Give past thought risk law to cultural.\r\nMachine address word any establish those.\r\nWith send attention beautiful current. Song bring type. Phone yard market reach ok word.",
"is_featured": true,
"is_active": true,
"images": [
{
"id": 1,
"image": null,
"variant": 58,
"is_primary": false,
"created_at": "11-22-2024 09:55:11"
},
{
"id": 2,
"image": null,
"variant": 60,
"is_primary": false,
"created_at": "11-22-2024 09:56:28"
}
],
"variants": [
{
"id": 58,
"color": "Red",
"value": "Late Kurtas - Red",
"variant_price": "443.00",
"stock_quantity": 58,
"is_available": true,
"is_active": true,
"created_at": "11-19-2024 07:09:04",
"updated_at": "11-19-2024 07:09:04",
"product": 21,
"size": {
"id": 4,
"size": "Extra Large",
"value": "XL"
},
"images": [
{
"id": 1,
"image": null,
"variant": 58,
"is_primary": false,
"created_at": "11-22-2024 09:55:11"
}
],
"primary_image": null
},
{
"id": 60,
"color": "Green",
"value": "Late Kurtas - Green",
"variant_price": "509.00",
"stock_quantity": 33,
"is_available": true,
"is_active": true,
"created_at": "11-19-2024 07:09:04",
"updated_at": "11-19-2024 07:09:04",
"product": 21,
"size": {
"id": 5,
"size": "Extra Small",
"value": "XS"
},
"images": [
{
"id": 2,
"image": null,
"variant": 60,
"is_primary": false,
"created_at": "11-22-2024 09:56:28"
}
],
"primary_image": null
}
],
"created_at": "11-19-2024 07:09:04",
"updated_at": "11-29-2024 08:19:06"
},
],
}
]
}
]
}