Question

Using a ManyToMany field as part of a custom Django CMS plugin may display when Edit Mode is turned on, but once published, the template will behave as if there are no associated objects in the field. For example, if you have authorized_personnel = models.ManyToManyField(Employee, blank=True, verbose_name='Authorized Personnel' as part of your defined model in models.py, {{ instance.authorized_personnel.all }} in the template returns [] in the published view, even if it works as expected in the Edit Mode view and returns a list populated from your database. How do you fix this?

Was it helpful?

Solution

For the custom model, a copy_relations(self, oldinstance) method must be defined as part of the model. In this case, you'd use something like:

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

This provides the information that Django needs when it makes a published copy of the plugin instance. For more information, see the documentation: http://docs.django-cms.org/en/latest/extending_cms/custom_plugins.html#handling-relations

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top