문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top