【问题标题】:Duplicate model-objects when save plugins in django-cms在 django-cms 中保存插件时重复模型对象
【发布时间】: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'

谢谢!

【问题讨论】:

标签: python django django-models django-cms


【解决方案1】:

这是 CMS 新手的常见误解。

一旦您发布了一个页面,它的副本以及附加到它的所有内容(插件等)都会创建为“实时”版本。

然后,您可以对页面草稿版本中的页面进行编辑,附带插件的草稿版本等。草稿版本发布后,新副本将替换旧的实时版本。看看publishing docs

不用担心,也不要向管理员公开插件模型,因为它们应该通过 CMS 前端进行编辑。

【讨论】:

  • 好的,但是如何复制相关对象 (Service)。它们不是通过 CMS 前端编辑的,而是在每个插件保存后克隆
  • 我不太确定,因为您已将 ServicePluginRelation 作为插件的内联包含在其中。我必须看到事情正在运行并查看数据库才能确定,但​​我认为这仍然是草稿和实时版本的情况,因为你已经内联了。
猜你喜欢
  • 2021-03-24
  • 2016-08-27
  • 2020-02-20
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 2022-10-14
相关资源
最近更新 更多