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, we will see how that can be used in practice. We will create an app that shows viral videos and counts how many times each video has been seen on mobile and desktop devices.
To start with, install django-mobile
to your virtual environment. This module will be necessary to differentiate between desktop devices and mobile devices:
(myproject_env)$ pip install django-mobile
To configure it, you will need to modify several project settings as follows. Besides that, let's create the viral_videos
app. Put both of them under INSTALLED_APPS
:
# conf/base.py or settings.py INSTALLED_APPS = ( # ... # third party "django_mobile", # project-specific "utils", "viral_videos...