- In your
settings.py
change the location of the migrations folder for the django.contrib.flatpages
app to another folder than the default one. For example:
settings.py
:
MIGRATION_MODULES = {'flatpages': 'yourproject.flatpages.migrations'}
Keep in mind that you have to create empty __init__.py
files within the folders yourproject
, flatpages
and migrations
to make Python
treat those dictionaries as packages.
Execute the makemigrations
management command to populate the initial migration to your MIGRATION_MODULES
folder. It should look similar to the default one.
Within the apps.py
of one of your apps (preferably the one using the flatpages feature) add oggy's class_prepared
solution:
apps.py
:
from django.apps import AppConfig
from django.db.models.signals import class_prepared
from django.db import models
def alter_flatpages(sender, **kwargs):
if sender.__module__ == 'django.contrib.flatpages.models' and sender.__name__ == 'FlatPage':
meta_description = models.CharField(max_length=255, blank=True, null=True)
meta_description.contribute_to_class(sender, 'meta_description')
class TexteConfig(AppConfig):
name = 'marlene.texte'
def __init__(self, app_name, app_module):
super().__init__(app_name, app_module)
class_prepared.connect(alter_flatpages)
Add another migration by executing makemigrations
again. This time you have added the CharField
meta_description
to the model. migrate
to apply the changes to the database.
Modify the FlatPageAdmin
:
admin.py
:
from django.contrib.flatpages.admin import FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
class MarleneFlatPageAdmin(FlatPageAdmin):
fieldsets = (
(None, {'fields': ('url', 'title', 'content', 'meta_description', 'sites')}),
(_('Advanced options'), {
'classes': ('collapse',),
'fields': ('registration_required', 'template_name'),
}),
)
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, MarleneFlatPageAdmin)
The biggest drawback of this solution is that if Django
adds new migrations to the flatpage app in the future you are responsible for managing them. I would advise everyone not to use the flatpage app no matter if seems a good fit for your current situation.