0
0
Djangoframework~30 mins

Field lookups (exact, contains, gt, lt) in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Filtering Django QuerySets with Field Lookups
📖 Scenario: You are building a simple Django app to manage a bookstore's inventory. You want to filter books based on different criteria like exact title, partial author name, price greater than a value, and publication year less than a value.
🎯 Goal: Learn how to use Django ORM field lookups exact, contains, gt, and lt to filter QuerySets.
📋 What You'll Learn
Create a Django model Book with fields title, author, price, and year_published
Create a QuerySet variable books with some sample book objects
Create a variable price_threshold set to 20
Filter books with exact title 'Django Basics' using exact lookup
Filter books with author name containing 'Smith' using contains lookup
Filter books with price greater than price_threshold using gt lookup
Filter books published before year 2010 using lt lookup
💡 Why This Matters
🌍 Real World
Filtering data in Django apps is essential for showing users only relevant information, like books matching their search criteria.
💼 Career
Understanding Django ORM field lookups is a key skill for backend developers working with Django to build efficient and readable database queries.
Progress0 / 4 steps
1
Create the Book model and sample data
Create a Django model class called Book with fields title (CharField), author (CharField), price (FloatField), and year_published (IntegerField). Then create a QuerySet variable called books with these exact book instances: Book(title='Django Basics', author='John Smith', price=15.99, year_published=2015), Book(title='Advanced Django', author='Jane Doe', price=25.50, year_published=2018), Book(title='Python 101', author='Mike Smith', price=19.99, year_published=2008), Book(title='Web Development', author='Anna Johnson', price=22.00, year_published=2005).
Django
Need a hint?

Use Django model fields like CharField for text, FloatField for price, and IntegerField for year. Create a list of Book instances for books.

2
Set the price threshold variable
Create a variable called price_threshold and set it to the float value 20.0.
Django
Need a hint?

Just create a variable named price_threshold and assign it the value 20.0.

3
Filter books using field lookups
Create four new variables to filter the books list using Django-like field lookups: exact_title for books with title exactly 'Django Basics' using exact lookup, author_contains_smith for books with author containing 'Smith' using contains lookup, price_gt_threshold for books with price greater than price_threshold using gt lookup, and year_lt_2010 for books published before 2010 using lt lookup. Use list comprehensions to simulate these filters.
Django
Need a hint?

Use list comprehensions with conditions matching the field lookups: == for exact, in for contains, > for gt, and < for lt.

4
Complete the filtering with Django QuerySet style
Add a final line that creates a Django QuerySet-like filter call named filtered_books that chains all four filters using Django ORM syntax: filter books with title exactly 'Django Basics', author containing 'Smith', price greater than price_threshold, and year published less than 2010. Use the Django QuerySet filter() method with keyword arguments and chaining.
Django
Need a hint?

Use Django ORM chaining with filter() and double underscores for lookups like title__exact, author__contains, price__gt, and year_published__lt.