如何复制内联对象来生存?
How to duplicate inline objects to live?
我是 Django CMS 的新手。我创建了一个带有内联的插件。
当我保存插件和发布页面时,模型会被复制,但内联对象不会被复制。
有什么办法吗?当我发布页面时,我希望我的内联对象也保存在活动对象中。
您需要在您的插件上实现一个 copy_relations
方法。文档是 here,但基本上功能是这样工作的;
class ArticlePluginModel(CMSPlugin):
title = models.CharField(max_length=50)
def copy_relations(self, oldinstance):
# Before copying related objects from the old instance, the ones
# on the current one need to be deleted. Otherwise, duplicates may
# appear on the public version of the page
self.associated_item.all().delete()
for associated_item in oldinstance.associated_item.all():
# instance.pk = None; instance.pk.save() is the slightly odd but
# standard Django way of copying a saved model instance
associated_item.pk = None
associated_item.plugin = self
associated_item.save()
class AssociatedItem(models.Model):
plugin = models.ForeignKey(
ArticlePluginModel,
related_name="associated_item"
)
我是 Django CMS 的新手。我创建了一个带有内联的插件。 当我保存插件和发布页面时,模型会被复制,但内联对象不会被复制。
有什么办法吗?当我发布页面时,我希望我的内联对象也保存在活动对象中。
您需要在您的插件上实现一个 copy_relations
方法。文档是 here,但基本上功能是这样工作的;
class ArticlePluginModel(CMSPlugin):
title = models.CharField(max_length=50)
def copy_relations(self, oldinstance):
# Before copying related objects from the old instance, the ones
# on the current one need to be deleted. Otherwise, duplicates may
# appear on the public version of the page
self.associated_item.all().delete()
for associated_item in oldinstance.associated_item.all():
# instance.pk = None; instance.pk.save() is the slightly odd but
# standard Django way of copying a saved model instance
associated_item.pk = None
associated_item.plugin = self
associated_item.save()
class AssociatedItem(models.Model):
plugin = models.ForeignKey(
ArticlePluginModel,
related_name="associated_item"
)