Create a model admin on Wagtail - scieloorg/template-scms GitHub Wiki
Example model
To create a model in Django is necessary to define an application that is the same in Wagtail.
Create any app in Wagtail:
make django_bash
In container execute
python manage.py startapp source_directory
The name of our app is source_directory, the acronym to Data Source Directory
After this command note that was create a folder call source_directory
cd source_directory
In this directoy we have a models.py
access this file and add the follow lines:
from django.db import models
from django.utils.translation import gettext as _
from wagtail.admin.edit_handlers import FieldPanel
from . import choices
class SourceDirectory(models.Model):
name = models.CharField(_("Name"), max_length=255, null=False, blank=False)
link = models.URLField("Link", null=False, blank=False)
source_type = models.CharField(_("Source Type"), max_length=255,
choices=choices.SOURCE_TYPE,
null=True, blank=True)
panels = [
FieldPanel('name'),
FieldPanel('link'),
FieldPanel('source_type')
]
It necessary create a file ``choices.py```
SOURCE_TYPE = [
("", ""),
("Primary", "primary"),
("Secondary", "secondary"),
("Tertiary", "tertiary"),
]
Add a new file call wagtail_hooks.py (This file will define a new enter in the Wagtail admin interface)
from django.utils.translation import gettext as _
from wagtail.contrib.modeladmin.options import (ModelAdmin, modeladmin_register)
from .models import SourceDirectory
class SourceDirectoryAdmin(ModelAdmin):
model = SourceDirectory
menu_label = 'Data Source Directory' # ditch this to use verbose_name_plural from model
menu_icon = 'pilcrow' # change as required
menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
add_to_settings_menu = False # or True to add your model to the Settings sub-menu
exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
list_display = ('name', 'source_type')
list_filter = ('source_type',)
search_fields = ('name', 'source_type')
# Now you just need to register your customised ModelAdmin class with Wagtail
modeladmin_register(SourceDirectoryAdmin)
Add this app ("source_directory"
) to THIRD_PARTY_APPS
in config/settings/base.py
THIRD_PARTY_APPS = [
"crispy_forms",
"crispy_bootstrap5",
"allauth",
"allauth.account",
"allauth.socialaccount",
"django_celery_beat",
"captcha",
"wagtailcaptcha",
"wagtailmenus",
"rest_framework",
"source_directory",
]
Now, it time to migrate to add this fields in database:
make django_makemigrations
make django_migrate
This will generate the migrate file:
# Generated by Django 3.2.12 on 2022-06-09 18:17
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='SourceDirectory',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, verbose_name='Nome')),
('link', models.URLField(verbose_name='Link')),
('source_type', models.CharField(choices=[('', ''), ('Primary', 'primary'), ('Secondary', 'secondary'), ('Tertiary', 'tertiary')], max_length=255, verbose_name='Source Type')),
],
),
]
After this steps is enable a new option in the Wagtail admin menu, look: