Extending Entry Page

Puput allows extend the EntryPage model. It provides two approaches to extend entries depending on the project requirements.

Multi-table inheritance

The easiest way to extend EntryPage model is using multi-table inheritance. Imagine if you need an special entry that needs a mandatory video url. You can write an entry model like this on models.py of your project:

from django.db import models
from puput.models import EntryPage


class VideoEntryPage(EntryPage):
    video_url = models.URLField()
    content_panels = EntryPage.content_panels + [
        FieldPanel('video_url')
    ]

You also need to modify subpage_types field of BlogPage model as by default is bounded to have only EntryPage as children. You can rewrite the above example with this:

from django.db import models
from puput.models import EntryPage, BlogPage


class VideoEntryPage(EntryPage):
    video_url = models.URLField()
    content_panels = EntryPage.content_panels + [
        FieldPanel('video_url')
    ]
BlogPage.subpage_types.append(VideoEntryPage)

This will create two independent tables on the database so you can create entries on your blog that are instances of EntryPage or VideoEntryPage.

Abstract base classes

Another approach to have an extension of entries is using abstract base classes inheritance method by inheriting from EntryAbstract instead of EntryPage. In the previous example, it’s shown a blog with regular entries (EntryPage) and tv entries (VideoEntryPage). If you only want to have VideoEntryPage on your blog and create a simple table you need to extend EntryAbstract model on models.py of your project.

from django.db import models
from puput.abstracts import EntryAbstract
from wagtail.wagtailadmin.edit_handlers import FieldPanel


class VideoEntryAbstract(EntryAbstract):
    video_url = models.URLField()

    content_panels = [
        FieldPanel('video_url')
    ]

    class Meta:
        abstract = True

Warning

Do not import the EntryPage model in your models.py where defining the abstract extended model because it will cause a circular importation.

Registering entry extension

You have to register the model extension in settings.py adding PUPUT_ENTRY_MODEL with the path of the abstract model.

Following the previous example you have to add PUPUT_ENTRY_MODEL in your settings.py file:

PUPUT_ENTRY_MODEL = 'youproject.models.VideoEntryAbstract'

Migrations

If you extend EntryPage model you must migrate the database in order to the see the changes that you made on the model. However if you perform a makemigrations operation it will create a migration in puput.migrations of your local Puput module folder.

So you need to define a new path to store the changes made on EntryPage model extension. You have to use MIGRATION_MODULES for this purpose:

MIGRATION_MODULES = {'puput': 'youproject.puput_migrations'}

After run makemigrations puput migrations will appear on puput_migrations folder.

Note

It’s recommended that the new initial migration represents the initial Puput migration in order to avoid conflicts when applying migrate puput command. A recommend way is run makemigrations puput before define Entry model extension on settings.py by setting PUPUT_ENTRY_MODEL.