Reputation: 32
I've built a custom plugin for a Django CMS project which I've named PageBlockPlugin
. The associated PageBlock
model features an HTML field, and I've defined a __str__
method on the model which truncates the HTML content to the first three words. When I'm in the Django admin view for this model, I see a list of instances which I've placed in one of my templates:
This looks great! However, when I'm in the frontend editor for the same template and switch to "structure" view, what I see is just a list of instance ids:
I've looked at a number of plugin examples and haven't been able to figure out how to use a model's string representation for it's associated plugin. Any ideas?
Here's a stripped-down version what I've defined so far:
models.py
class PageBlock(CMSPlugin):
content = HTMLField()
def __str__(self):
return Truncator(strip_tags(self.content)).words(3, truncate="...")
cms_plugins.py
class PageBlockPlugin(CMSPluginBase):
model = models.PageBlock
name = "Page Block"
render_template = "some_template.html"
def render(self, context, instance, placeholder):
context["instance"] = instance
return context
plugin_pool.register_plugin(PageBlockPlugin)
admin.py
class PageBlockAdmin(FrontendEditableAdminMixin, admin.ModelAdmin):
pass
admin.site.register(models.PageBlock, PageBlockAdmin)
Upvotes: 0
Views: 352