As tumblelogs are comprised of a stream of posts, each with a unique strucutre and schema, it is impractical for a single model to represent all types of posts. tumblelog allows you to develop separate models for each post type while maintaining the ability to query all posts at a time, regardless of the post type.
Post types are subclasses of tumblelog.models.BasePostType, which is itself a subclass of Django’s db.models.Model. Let’s look at an example post type for recipes, which might be defined in the models module of an app called recipes:
from django.db import models
from django.utils.translation import ugettext as _
from tumblelog.models import BasePostType
class Recipe(BasePostType):
"""
Post type for a recipe.
"""
intro = models.TextField(_('Introduction'))
prep_time = models.CharField(_('Preparation Time'), max_length=128)
cook_time = models.CharField(_('Cooking Time'), max_length=128)
recipe_yield = models.CharField(_('Yield'), max_length=128)
ingredients = models.TextField(_('Ingredients'))
directions = models.TextField(_('Directions'))
class TumblelogMeta:
prepopulated_fields = {
'slug': ('title',)
}
In addition to fields, you may add model methods, custom managers, or a Meta inner class. Anything you can do with regular Django models is fair game.
You should not create a ModelAdmin for post type classes. Instead, you can define ModelAdmin properties on inner class TumblelogMeta (as is done for prepopulated_fields in the example). These are, in turn, used to dynamically generate a ModelAdmin.
BasePostType provides subclasses with each of these fields:
These properties are available to each BasePostType subclass:
When an instance of a post type is first saved, a corresponding Post object is created. Post objects contain a generic foreign key to an instance of a post type, allowing you to query all post types at once.
>>> from tumblelog.models import Post
>>> Post.objects.public()
[<Post: Deboning Pony Legs (Text)>, <Post: Crostini with Pony Tartare (Recipe)>, <Post: Pony Au Poivre (Recipe)>, <Post: Pony Stew with Dried Plums (Recipe)>]
Note that the query returned instances of both Text and Recipe post types.
Each time a post type object is saved, the values of the slug, status, date_published, date_added, and date_modified fields are copied from the object to its associated Post object, allowing field lookups of each of these fields:
Post‘s default manager is extended to include the following methods: