Denis Komogortsev
Denis Komogortsev

Reputation: 31

Django-cms plugin for existing model

I have a django project with 'news' applications. here is the model:

class News(models.Model):
   title = CharField(max_length=255)
   content = TextField()

How can I add django-cms to this project to be able modify news directly in template? I know I need a custom plugin based on 'news' model, and here I have two choices: 1) I can describe all fields of 'news' model in single plugin, but then there will be a window to edit all fields at once.

plugin, that describes all fields:

class NewsPlugin(CMSPlugin):
    title = CharField(max_length=255)
    content = TextField()
    def __unicode__(self):
        return self.title

2) The second idea is about to write plugins for each separate field in 'news' and combine them in some another plugin, but I dont know how to realize it.

separate plugin for news title:

class NewsTitle(CMSPlugin):
    title = CharField(max_length=255)

separate pluginn for news content:

class NewsContent(CMSPlugin):
    content = TextField()

Any idea?

Upvotes: 2

Views: 805

Answers (2)

Daniele Procida
Daniele Procida

Reputation: 1539

Use plugins to deliver news articles from your application into other django CMS content - don't use plugins to create news content.

See the django CMS documentation on how to use placeholders in other applications.

Upvotes: 0

markwalker_
markwalker_

Reputation: 12869

What you might want to consider is an alternative to plugins which I find works very well with a news application.

You could change your news content field to a PlaceholderField which will allow you to add plugin's to a news item and also configure the placeholder config with all the usual options available to standard template placeholders.

In my news app I have a fairly typical setup, a ListView and then a DetailView where you can switch the CMS to edit mode & edit the plugins in your news item's PlaceholderField.

You could also extend the toolbar to offer links to add new news items in a modal dialog, or list the existing news items;

@toolbar_pool.register
class LatestNewsToolbar(CMSToolbar):

    def populate(self):

        news_menu = self.toolbar.get_or_create_menu(
            NEWS_MENU_IDENTIFIER, NEWS_MENU_NAME
        )
        position = news_menu.get_alphabetical_insert_position(
            _('Latest news'),
            SubMenu
        )

        menu = news_menu.get_or_create_menu(
            'latest_news_menu',
            _('Latest News ...'),
            position=position
        )
        try:
            menu.add_modal_item(
                _('Add News Item'),
                url=admin_reverse('news_latestnews_add')
            )
        except NoReverseMatch:
            # not in urls
            pass
        try:
            menu.add_modal_item(
                _('Existing News Items'),
                url=admin_reverse('news_latestnews_changelist')
            )
        except NoReverseMatch:
            # not in urls
            pass

    def post_template_populate(self):
        pass

    def request_hook(self):
        pass

If you have a play with this way of working, I think you'll find it more suitable & more powerful than plugins :)

And checkout this video on this area of CMS; https://www.youtube.com/watch?time_continue=2670&v=Dj8dhgmzlFM

Upvotes: 1

Related Questions