Getting ready

Once again, we will start with the core app, which should be installed and ready for custom template tags.

In addition, to illustrate the concept, let's create a news app with an Article model, as follows:

# myproject/apps/news/models.py
from django.db import models
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _

from myproject.apps.core.models import CreationModificationDateBase, UrlBase


class ArticleManager(models.Manager):
def random_published(self):
return self.filter(
publishing_status=self.model.PUBLISHING_STATUS_PUBLISHED,
).order_by("?")


class Article(CreationModificationDateBase, UrlBase):
PUBLISHING_STATUS_DRAFT, PUBLISHING_STATUS_PUBLISHED = "d", "p"
PUBLISHING_STATUS_CHOICES = (
(PUBLISHING_STATUS_DRAFT, _("Draft")),
(PUBLISHING_STATUS_PUBLISHED, _("Published")),
)
title = models.CharField(_("Title"), max_length=200)
slug = models.SlugField(_("Slug"), max_length=200)
content = models.TextField(_("Content"))
publishing_status = models.CharField(
_("Publishing status"),
max_length=1,
choices=PUBLISHING_STATUS_CHOICES,
default=PUBLISHING_STATUS_DRAFT,
)

custom_manager = ArticleManager()

class Meta:
verbose_name = _("Article")
verbose_name_plural = _("Articles")

def __str__(self):
return self.title

def get_url_path(self):
return reverse("news:article_detail", kwargs={"slug": self.slug})

There, the interesting part is the custom_manager for the Article model. The manager can be used to list out random published articles.

Using the examples of the previous chapter, you can complete the app with URL configurations, views, templates, and administration setup. Then, add some articles to the database using the administration form.

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

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