こんにちわ、ゆきねこです。
引き続き、wagtailについて書いていきます。
コンテンツマネジメントシステムということで新しくデータを登録することがあるかと思うので、その方法について書いていきます。
まず、INSTALLED_APPSにwagtail.contrib.modeladminを加えます。
INSTALLED_APPS = [ ... 'wagtail.contrib.modeladmin', ... ]
次に、データモデルを作成します。
ユーザモデルと同じようにアプリから作成します。
コーヒーが好きなのでpython manage.py startapp coffeesって感じですかね
で、そのあとこんな感じでmodelを作っていきます。
from django.db import models
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.images.edit_handlers import ImageChooserPanel
class Coffee(models.Model):
name = models.CharField(max_length=255)
country = models.CharField(max_length=255)
taste = models.CharField(max_length=255)
buy_date = models.DateField("Buy date")
coffee_photo = models.ForeignKey(
'wagtailimages.Image',
null=True, blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
panels = [
FieldPanel('name'),
FieldPanel('country'),
FieldPanel('taste'),
FieldPanel('buy_date'),
ImageChooserPanel('coffee_photo')
]
作ったらINSTALLED_APPSに登録してマイグレーションします。
その後、coffees/admin.pyに以下のようにします。
from django.contrib import admin
from wagtail.contrib.modeladmin.options import (
ModelAdmin, modeladmin_register)
from .models import Coffee
class CoffeeAdmin(ModelAdmin):
model = Coffee
menu_label = 'Coffee'
menu_icon = 'pilcrow'
menu_order = 200
add_to_settings_menu = False
exclude_from_explorer = False
list_display = ('name', 'country', 'taste', 'buy_date', 'coffee_photo')
list_filter = ('name',)
search_fields = ('name', 'country', 'taste', 'buy_date', 'coffee_photo')
modeladmin_register(CoffeeAdmin)
要はModelAdminを拡張するかんじですね。
その結果、下のように加わります。

こんな感じです。
0件のコメント