Reputation: 127
My model:
class Post(models.Model):
title = models.CharField(max_length=100)
short_description = models.CharField(max_length=100)
image = models.ImageField(upload_to="uploads/images/")
content = HTMLField(blank=True)
slug = AutoSlugField(always_update=True,populate_from='title', unique=True)
date_created = models.DateField(default=datetime.date.today())
CMS Plugin:
class PostPlugin(CMSPlugin):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
Register plugin:
@plugin_pool.register_plugin
class CMSPostPlugin(CMSPluginBase):
model = PostPlugin
name = _("Post")
render_template = "post/post.html"
allow_children = True
admin_preview = True
module = "subpage"
def render(self, context, instance, placeholder):
context.update({
'post':instance.post,
'instance':instance,
'placeholder':placeholder
})
return context
So, after that, I add this to my database, the intermediate table was created. Here a screenshot. table screenshot
This is how I get the plugin instances:
CMSPlugin.objects.filter(plugin_type='CMSPostPlugin', placeholder_id=placehoder.id)
Subsequently, I want to get the Post
model from this intermediate table, but I don't know how to do that.
Of course, I can hardcode it like get a table by name and then get post id, but maybe there is a "normal" solution for Django CMS?
Upvotes: 2
Views: 317
Reputation: 5303
You should be able to do something like this:
plugin_instance = (
CMSPlugin.objects
.filter(plugin_type='CMSPostPlugin', placeholder_id=placehoder.id)
.first()
)
post_model_instance = PostModel.get(id=plugin_instance.id)
post = post_model_instance.post
It seems that the id
s of the CMSPlugin
instance and its model are identical.
Upvotes: 1