Problem statement
The product search endpoint is wired up, but the function behind it, search_products in app/views.py, is an empty stub. The Product model, the GET /products/search/ endpoint and its JSON response are provided. Your task is to implement search_products so the database does the searching: a case-insensitive "name contains" match, an inclusive price range, the exclusion of inactive items, and a fixed result order - all in a single query.
Sample Data
The provided Product model (app/models.py) has name (CharField), price (DecimalField, 2 decimal places) and is_active (BooleanField, default True). Example rows:
| name | price | is_active |
|---|---|---|
| Gaming Laptop | 1200.00 | True |
| Laptop Stand | 45.00 | True |
| LAPTOP SLEEVE | 25.00 | True |
| Wireless Mouse | 25.00 | True |
| USB-C Cable | 9.99 | True |
| Refurbished Laptop | 450.00 | False |
| Mouse Pad | 5.00 | False |
Requirements
Only app/views.py needs to change: implement search_products(name=None, min_price=None, max_price=None) and return the matching products. The provided product_search view reads the name, min_price and max_price query parameters (an omitted or empty parameter is passed as None), calls search_products, and returns 200 with {"count": <n>, "results": [{"id", "name", "price", "is_active"}, ...]} in the order search_products returns them. Query parameters are assumed to be valid numbers where a price is expected.
1. Name contains (case-insensitive)
- When name is given, keep only products whose name contains it anywhere, ignoring case on both sides (laptop, LAPTOP and lApToP all match Gaming Laptop and LAPTOP SLEEVE).
- When name is None, do not filter by name.
2. Price range
- When min_price is given, keep only products whose price is at least min_price.
- When max_price is given, keep only products whose price is at most max_price.
- Both bounds are inclusive, and either may be given on its own.
3. Combined filters intersect
- Every given filter applies at once: a product is returned only if it satisfies all of them. Filters that share no products return an empty result.
4. Exclude inactive items
- Products with is_active=False are never returned, even when they match every filter. Only inactive products are removed; every active product that matches the filters is returned.
5. Order
- Results are ordered by price ascending (cheapest first); products with the same price are ordered by name ascending.
6. One query, in the database
- search_products returns a Django QuerySet (not a list).
- Calling search_products and then evaluating the queryset it returns runs exactly one database query in total, and that query does all of the filtering and ordering. Filtering and sorting must not be done in Python.
Sample Requests & Responses
GET /products/search/?name=LAPTOP
{
"count": 3,
"results": [
{"id": 3, "name": "LAPTOP SLEEVE", "price": "25.00", "is_active": true},
{"id": 2, "name": "Laptop Stand", "price": "45.00", "is_active": true},
{"id": 1, "name": "Gaming Laptop", "price": "1200.00", "is_active": true}
]
}
GET /products/search/?min_price=25.00&max_price=45.00
{
"count": 3,
"results": [
{"id": 3, "name": "LAPTOP SLEEVE", "price": "25.00", "is_active": true},
{"id": 4, "name": "Wireless Mouse", "price": "25.00", "is_active": true},
{"id": 2, "name": "Laptop Stand", "price": "45.00", "is_active": true}
]
}
GET /products/search/?name=laptop&min_price=30&max_price=500
{
"count": 1,
"results": [
{"id": 2, "name": "Laptop Stand", "price": "45.00", "is_active": true}
]
}
GET /products/search/?name=mouse
{
"count": 1,
"results": [
{"id": 4, "name": "Wireless Mouse", "price": "25.00", "is_active": true}
]
}
Mouse Pad matches the name but is inactive, so it is excluded. In the price-range example, LAPTOP SLEEVE and Wireless Mouse share a price, so they are ordered by name.
