【发布时间】:2015-09-11 07:34:31
【问题描述】:
我开始使用 django-cms 遇到了一些问题。
第一个问题:
当我使用插件 (CMSServicesListPlugin) 保存页面时,在管理端我看到相关对象的重复项。它们具有相同的 id。如何隐藏重复或更好地禁用创建它
第二个问题:
当我将带有 ForeignKey-field 的插件更改为 cms.Page (CMSBannerWithImagePlugin) 时,我在 select 中看到了 Page 的重复项。如何解决它的问题?
我的模型:
# models.py
class Service(models.Model):
title = models.CharField(blank=False, null=False, max_length=255)
class ServicePluginRelation(models.Model):
service = models.ForeignKey('services.Service', blank=False, null=False, related_name='relations')
plugin = models.ForeignKey('services.ServicesListPlugin', blank=False, null=False, related_name='relations')
class ServicesListPlugin(CMSPlugin):
title = models.CharField(blank=True, null=True, max_length=255)
services = models.ManyToManyField('services.Service', related_name='plugins', blank=False, null=False, through=ServicePluginRelation)
def copy_relations(self, oldinstance):
for relation in oldinstance.relations.all() :
relation.plugin = self
relation.pk = None
relation.id = None
relation.save()
class BannerWithImage(CMSPlugin):
image = models.ImageField(blank=True, null=True, upload_to='links/link_with_image/image')
page = models.ForeignKey('cms.Page', blank=False, null=False)
还有普金斯:
# cms_plugins.py
class ServicePluginRelationInlineAdmin(SortableTabularInline):
model = ServicePluginRelation
class CMSServicesListPlugin(CMSPluginBase):
model = ServicesListPlugin
inlines = (ServicePluginRelationInlineAdmin,)
def get_render_template(self, context, instance, placeholder):
return 'services/services_list.html'
class CMSBannerWithImagePlugin(CMSPluginBase):
model = BannerWithImage
render_template = 'links/link_with_image.html'
谢谢!
【问题讨论】:
-
显示你的 CMSPluginBase
-
@Sławek 这是 django-cms 类 github.com/divio/django-cms/blob/develop/cms/plugin_base.py#L95
标签: python django django-models django-cms