Django Object-Relational Mapping (ORM) comes with special abstraction constructs that can be used to build complex database queries. They are called query expressions, and they allow you to filter data, order it, annotate new columns, and aggregate relations. In this recipe, you will see how these can be used in practice. We will create an app that shows viral videos and counts how many times each video has been seen by anonymous or logged-in users.
Using database query expressions
Getting ready
To start with, create a viral_videos app with a ViralVideo model and set up the system so that it logs to a log file by default:
Create the viral_videos app and add it under INSTALLED_APPS in the settings:
# myproject/settings/_base...