0
0
Djangoframework~30 mins

Why advanced DRF features matter in Django - See It in Action

Choose your learning style9 modes available
Why advanced DRF features matter
📖 Scenario: You are building a simple API for a bookstore. You want to show how using advanced Django REST Framework (DRF) features can make your API better and easier to maintain.
🎯 Goal: Create a basic DRF API with a book list, then add a filter feature to show only books with more than 300 pages. This shows why advanced DRF features like filtering matter.
📋 What You'll Learn
Create a list of books as Python dictionaries
Add a variable to set the minimum page count filter
Use a list comprehension to filter books with pages greater than the minimum
Return the filtered list in a DRF API view
💡 Why This Matters
🌍 Real World
Filtering data in APIs is common in real-world apps like bookstores, libraries, or any service showing lists of items.
💼 Career
Understanding DRF filtering and API views is essential for backend developers building REST APIs with Django.
Progress0 / 4 steps
1
Create the initial book data
Create a variable called books that is a list of dictionaries. Each dictionary should have these exact entries: {'title': 'Book A', 'pages': 250}, {'title': 'Book B', 'pages': 320}, and {'title': 'Book C', 'pages': 150}.
Django
Need a hint?

Use a list with three dictionaries exactly as shown.

2
Add a minimum page count filter variable
Create a variable called min_pages and set it to 300.
Django
Need a hint?

Just create a variable named min_pages and assign 300.

3
Filter books with pages greater than min_pages
Create a variable called filtered_books that uses a list comprehension to include only books from books where the pages value is greater than min_pages.
Django
Need a hint?

Use a list comprehension with book for book in books if book['pages'] > min_pages.

4
Create a simple DRF API view to return filtered books
Create a DRF API view function called book_list that returns a Response with filtered_books. Import Response from rest_framework.response and api_view from rest_framework.decorators. Use the @api_view(['GET']) decorator.
Django
Need a hint?

Remember to import Response and api_view, then create a function book_list decorated with @api_view(['GET']) that returns Response(filtered_books).