How it works...

The {% load_objects %} template tag loads a QuerySet defined by the method of the manager from a specified app and model, limits the result to the specified count, and saves the result to the given context variable.

The following code is the simplest example of how to use the template tag that we have just created. It will load all news articles in any template, using the following snippet:

{% load utility_tags %}
{% load_objects all from news.Article as all_articles %}
{% for article in all_articles %}
    <a href="{{ article.get_url_path }}">{{ article.title }}</a>
{% endfor %}

This is using the all() method of the default objects manager of the Article model, and it will sort the articles by the ordering attribute defined in the Meta class of the model.

A more advanced example would be required to create a custom manager with a custom method to query the objects from the database. A manager is an interface that provides the database query operations to models. Each model has at least one manager called objects by default. As an example, let's create an Artist model that has a draft or a published status and a new custom_manager that allows you to select random published artists:

# artists/models.py
from django.db import models
from django.utils.translation import ugettext_lazy as _


STATUS_CHOICES = (
("draft", _("Draft")),
("published", _("Published")),
)


class ArtistManager(models.Manager):
def random_published(self):
return self.filter(status="published").order_by("?")


class Artist(models.Model):
# ...
status = models.CharField(_("Status"),
max_length=20,
choices=STATUS_CHOICES)
custom_manager = ArtistManager()

To load a random published artist, you add the following snippet to any template:

{% load utility_tags %}
{% load_objects custom_manager.random_published
from artists.Artist limit 1 as random_artists %} {% for artist in random_artists %} {{ artist.first_name }} {{ artist.last_name }} {% endfor %}
Template tags in the previous snippet have been split across lines for legibility, but in practice, template tags must be on a single line, and so cannot be split in this manner.

Let's look at the code of the {% load_objects %} template tag. In the parsing function, there are two allowed forms for the tag—with or without a limit. The string is parsed, and if the format is recognized, the components of the template tag are passed to the ObjectNode class.

In the render() method of the Node class, we check the manager's name and its method's name. If no manager is specified,  _default_manager will be used, which is an automatic property of any model injected by Django and points to the first available models.Manager() instance. In most cases, _default_manager will be the objects manager. After that, we will call the method of the manager and fall back to empty QuerySet if the method doesn't exist. If a limit is defined, we resolve the value of it and limit QuerySet accordingly. Lastly, we will save the resulting QuerySet to the context variable as given by var_name.

..................Content has been hidden....................

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