I am trying to setup a djangocms plugin to handle FAQ's. When I am in edit mode, everything works. I can see the FAQ's.
When I switch to published page, the FAQ's are not showing.
Looking at the queryset
in edit mode, I have the FAQ's but in published mode the FAQ's queryset is empty.
I tried to debug as best as I could with a lot of answers from stackoverflow but just can't find the error.
models.py
from django.db import models
from cms.models.pluginmodel import CMSPlugin
class FAQPluginModel(CMSPlugin):
title = models.CharField(max_length=255, default="FAQs")
def __str__(self):
return self.title
class FAQItem(models.Model):
plugin = models.ForeignKey(FAQPluginModel, related_name="faq_items", on_delete=models.CASCADE)
question = models.CharField(max_length=255)
answer = models.TextField()
def __str__(self):
return self.question
plugins.py
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from django.utils.translation import gettext_lazy as _
from .models import FAQPluginModel
from .admin import FAQPluginModelAdmin, FAQItemInline
class FAQPluginPublisher(CMSPluginBase):
model = FAQPluginModel
module = _("FAQs")
name = _("FAQ Plugin")
render_template = "faq_plugin.html"
# render_template = "faq_collapsible.html"
admin_preview = False
inlines = [FAQItemInline, ]
def render(self, context, instance, placeholder):
context.update({
'instance': instance,
'faq_items': instance.faq_items.all(),
})
print("faq_items:", context.get('faq_items'))
return context
plugin_pool.register_plugin(FAQPluginPublisher)
I have tried a lot of the posted solutions:
def copy_relations(self, oldinstance):
self.faq_items = oldinstance.faq_items.all()
This is the one which seemed the most relevant but can't get it to work.
Assuming you're using django-cms 3.x. Looking at one of my projects that has a similar data structure I did:
class FAQPluginModel(CMSPlugin):
title = models.CharField(max_length=255, default="FAQs")
faq_items = models.ManyToManyField(
FAQItem,
# I also defined a through model here
# through='SomeThroughModelNameHere',
related_name='faq_items',
blank=True,
)
def __str__(self):
return self.title
def copy_relations(self, oldinstance):
self.faq_items.set(oldinstance.faq_items.all())
Hope that helps.