我写了一个简单的插件,显示联系人,但我需要在某些页面上排除某些联系人。所以我在我的插件中添加了一个相关的模型,它使用了“structure_to_exclude”ManyToMany关系。我的问题是,当我得到这个变量时,它总是空的。
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)
相关模型
class VMContactCalendarPluginModel(CMSPlugin):
structure_to_exclude = models.ManyToManyField(
Structure,
verbose_name=_(u'Structures à exclure'),
)
结构模型(多态!!)
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']
打印结果:
实例:93
Instance.all()结果:[]
有什么想法吗?我试图用ID(93)检索插件实例,以确保它不是实例var的问题,但它不会改变任何东西...... 问候,知更鸟
答案 0 :(得分:2)
对于您创建的每个插件,一旦发布,就会有两个版本。 公众和草案版本。所以它对于改变它们来说非常好。
因为各个项目之间的关系各不相同,所以只要您的插件有关系,您就需要明确地告诉cms如何"复制"发布页面时的这些关系。
请调整您的插件模型以使用以下方法:
foo = zip([1,2,3], [1,2,3])
id(foo) == id(iter(foo)) # returns True in py3.5
您可以在我们的docs中了解有关插件关系的更多信息。
答案 1 :(得分:0)
我只是注意到在django-cms的“编辑”模式下,我的代码正在运行......
实例:92 Instance.all()结果:[Grammont>,Mayen>,Aï>,]
知道为什么它在正常模式下是bugy而不是在编辑时? 此致
编辑:实例ID发生变化,这是正常的吗?有人曾经有过这个问题吗?
编辑2:考虑到这个问题的性质,我在github divio repo上开了一个问题:https://github.com/divio/django-cms/issues/5747