【问题标题】:Copy tags to copied django object将标签复制到复制的 django 对象
【发布时间】:2012-09-21 10:23:04
【问题描述】:

我正在使用django-taggit 标记我的待办事项记录。

class Action(models.Model):
    name = models.CharField("Action Name", max_length=200)
    complete = models.BooleanField(default=False, verbose_name="Complete?")

    tags = TaggableManager()

我正在尝试制作记录的精确副本,一直到与任务关联的标签。

new_obj = deepcopy(self)
new_obj.id = None
new_obj.save()

运行此代码后,副本是准确的,只是没有附属标签。如何将所有标签从“self”复制到 new_obj?

【问题讨论】:

    标签: django django-taggit deep-copy


    【解决方案1】:

    而不是将标签添加到对象:

    new_obj.tags.add(tag)
    

    我将新对象添加到标签中:

    for tag in self.tags.all():
        tag_object = TaggedItem(content_object = new_obj, tag = tag)
        tag_object.save()
    

    【讨论】:

      猜你喜欢
      • 2014-07-31
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 2017-09-19
      • 2013-04-08
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      相关资源
      最近更新 更多