0

私はfeincmsを初めて使用し、拡張機能を作成しようとしています。非常に基本的なものを作成しました

from __future__ import absolute_import, unicode_literals

from django.db import models
from django.utils.translation import ugettext_lazy as _

from feincms import extensions


class Extension(extensions.Extension):
    def handle_model(self):
        self.model.add_to_class('excerpt', models.TextField(
            _('excerpt'),
            blank=True,
            help_text=_('Excerpts are good!')))

    def handle_modeladmin(self, modeladmin):
        modeladmin.add_extension_options(_('Exceprt'), {
            'fields': ('excerpt',),
            'classes': ('collapse',),
        })

テキスト フィールドの抜粋を追加しますが、もう少し複雑なものを書きたいと思います。地域のメディアを選択するのと同様のプロセスを使用して、単一の注目の画像をページに追加できるようにしたいのですが、これを行う方法がわかりません。この拡張機能に関するガイダンスをいただければ幸いです。

ありがとう!

4

1 に答える 1

1

あなたの質問を正しく理解しているかどうかはわかりませんが、これが役立つかもしれません: https://github.com/matthiask/feincms-in-a-box/blob/master/box/cms/models.py#L57 -- 追加するだけですMediaFileForeignKey を追加し、新しいフィールドを raw_id_fields に追加します。それだけです。

コード例は次のとおりです。

from __future__ import absolute_import, unicode_literals

from django.db import models
from django.utils.translation import ugettext_lazy as _

from feincms.module.page.models import Page
from feincms.extensions import Extension
from feincms.module.medialibrary.fields import MediaFileForeignKey
from feincms.module.medialibrary.models import MediaFile

class ExcerptExtension(Extension):
    def handle_model(self):
        self.model.add_to_class(
            'excerpt_image',
            MediaFileForeignKey(
                MediaFile, verbose_name=_('image'),
                blank=True, null=True, related_name='+'))
        self.model.add_to_class(
            'excerpt_text',
            models.TextField(_('text'), blank=True))

    def handle_modeladmin(self, modeladmin):
        modeladmin.raw_id_fields.append('excerpt_image')
        modeladmin.add_extension_options(_('Excerpt'), {
            'fields': ('excerpt_image', 'excerpt_text'),
        })


Page.register_extensions(
    ExcerptExtension,
)

: このコードをそのまま使用するには、最新バージョンの FeinCMS が必要です。より正確には、バージョン 1.9 以降のみが拡張クラスを (ドット付きの Python パスではなく) にPage.register_extensions直接渡すことをサポートしています。

于 2014-08-08T19:13:52.543 に答える