StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 2 · QuerySets & ORM Basics

Filtered Product Lookups

easy100%5 of 5 solved

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:

namepriceis_active
Gaming Laptop1200.00True
Laptop Stand45.00True
LAPTOP SLEEVE25.00True
Wireless Mouse25.00True
USB-C Cable9.99True
Refurbished Laptop450.00False
Mouse Pad5.00False

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.

More in QuerySets & ORM Basics

See the module
Ordered Result ShapingA report needs items in the right order, only a few columns, and just the latest rows. Add chainable QuerySet methods that sort by name or newest first, take the top N, and return plain dicts or a flat list of names.100%4 of 4 solvedeasySingle Object RetrievalDetail lookups crash on unknown products and silently pick a random row when names repeat. Fetch single products safely by sku and by name, ignore discontinued items, and answer "any products?" and "how many?" without loading rows.75%3 of 4 solvedeasyCatalog Query ToolboxSupport needs one set of catalog answers they can trust. Build query functions for search, newest items, price lists, name lists and single lookups, each skipping inactive products and running exactly one query.100%3 of 3 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.