Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering plugin variable issue - not rendering the same when in editing mode #5747

Closed
sulazix opened this issue Oct 31, 2016 · 4 comments
Closed

Comments

@sulazix
Copy link

sulazix commented Oct 31, 2016

I wrote a simple plugin which display contact people, but I need to exclude some contact on certain pages. So I added a related model to my plugin which use an "structure_to_exclude" ManyToMany relationship. My issue, when I get this variable it's allways empty.

The cms_plugins.py

class VMContactContactPlugin(CMSPluginBase):
    module = 'VM Contact Plugin'
    render_template = 'vm_contact/calendars/contacts_list.html'
    model = VMContactCalendarPluginModel
    name = _('VM Contact plugin')

    def render(self, context, instance, placeholder):
        print 'Instance : {0}'.format(instance)
        inst = instance.structure_to_exclude.all()
        print 'Instance.all() result : {0}'.format(inst)
        structures = Structure.objects.exclude(contact=None).exclude(pk__in=instance.structure_to_exclude.all().values_list('id',flat=True))
        context.update({
            'structures': structures,
        })
        return context

plugin_pool.register_plugin(VMContactContactPlugin)

The related model

 class VMContactCalendarPluginModel(CMSPlugin):
     structure_to_exclude = models.ManyToManyField(
         Structure,
         verbose_name=_(u'Structures à exclure'),
     )

The Structure Models (Polymorphic !!)

class Structure(PolymorphicModel):
    contact = models.ForeignKey(Contact, blank=True, null=True)
    members = models.ManyToManyField(Contact, blank=True, null=True, related_name='%(class)s_members')
    title = models.CharField(max_length=50, default='Castor')
    description = models.CharField(max_length=254, blank=True)
    categories = CategoryManyToManyField('aldryn_categories.Category',
                                         verbose_name=_('categories'),
                                         blank=True)
    calendars = models.ManyToManyField(Calendar, blank=True)
    has_pages = models.BooleanField(default=True)
    avatar = FilerFileField(null=True, blank=True,
                                 on_delete=models.SET_NULL)
    classcss =  models.CharField(max_length=1, choices=CSS_CLASS, default='5')
    order = models.PositiveSmallIntegerField(default=0)
    class Meta:
        ordering = ['order']

Print results :

Instance : 93
Instance.all() result : []

And When in "editing" mode of django-cms, my code is working...

Instance : 92
Instance.all() result : [ Grammont>, Mayen>, Aï>, ]

Any idea why it's bugy in normal mode and not when in editing ?

@sulazix
Copy link
Author

sulazix commented Oct 31, 2016

I just notice that Instance id changes... is that normal ?
Regards,
Robin

@czpython
Copy link
Contributor

Hello @sulazix,

For every plugin you create, there's two versions once you publish.
The public and the draft versions. So its perfectly fine for the ids to change.

Because relationships vary from project to project, anytime your plugin has relationships, you need to explicitly tell the cms how to "copy over" these relationships when publishing the page.

Please adapt your plugin model to have the following method:

def copy_relations(self, oldinstance):
    self.structure_to_exclude = oldinstance.structure_to_exclude.all()

You can read more about plugin relations in our docs.

@czpython
Copy link
Contributor

Closing in favor of stackoverflow post.

@sulazix
Copy link
Author

sulazix commented Oct 31, 2016

THANKS ! Sorry I missed this part of the documentation... my mistake...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants