How it works...

The list filter, based on the price (USD) field plus the custom one that we just created, will be shown in the sidebar of the product list, as follows:

The PhotoFilter class has translatable title and query parameter names as properties.
It also has two methods, as follows:

  • The lookups() method that defines the choices of the filter
  • The queryset() method that defines how to filter QuerySet objects when a specific value is selected

In the lookups() method, we define three choices, as follows:

  • There are no photos
  • There is one photo
  • There is more than one photo attached

In the queryset() method, we use the annotate() method of QuerySet to select the count of photos for each product. This count of the photos is then filtered according to the selected choice.

To learn more about the aggregation functions, such as annotate(), refer to the official Django documentation at https://docs.djangoproject.com/en/2.1/topics/db/aggregation/.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.37.68