0

I'm new to feincms and trying to write an extension, I wrote a very basic one

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',),
        })

to add a text field excerpt, but now I want to write one a bit more complex. I want to allow a single featured image to be added to a page using a process similar to selecting media for a region but I have zero clue how to go about this. Any guidance with this extension would be appreciated!

Thanks!

Jordan Brooklyn
  • 225
  • 1
  • 11

1 Answers1

1

not sure whether I understand your question correctly, but maybe this will help: https://github.com/matthiask/feincms-in-a-box/blob/master/box/cms/models.py#L57 -- just add a MediaFileForeignKey, and add the new field to raw_id_fields. That should be all.

Example code follows:

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,
)

Note: A recent version of FeinCMS is required for this code to work as-is. More precisely, only versions 1.9 and up support passing extension classes (instead of dotted Python paths) to Page.register_extensions directly.

Matthias Kestenholz
  • 3,300
  • 1
  • 21
  • 26