Using the Prefetch() class
The Prefetch()
class that is provided in the django.db.models
library is used to control how a prefetch_related()
operation is performed. For instance, we will use it to filter and show only vehicles
that are of the VehicleModel
that equals "Blazer LT"
. We can also prefetch all related objects when performing filters in this way. To learn about how to use this class in depth, visit https://docs.djangoproject.com/en/4.0/ref/models/querysets/#prefetch-objects.
Use the following subsections to prepare your view class and template for this demonstration. The URL pattern will remain the same as the demonstration found in the Sellers view subsection of this chapter.
Modifying the view
Follow these steps to modify your existing SellersView
class for this next exercise:
- In your
/chapter_10/views.py
file, add the followingimport
statement, preferably before the existingimport
statements:# /becoming_a_django_entdev/chapter_10/views.py from...