【问题标题】:django_mptt model with post_save signal带有 post_save 信号的 django_mptt 模型
【发布时间】:2015-08-20 10:55:43
【问题描述】:
class Category(MPTTModel):
    name = models.CharField(max_length=256)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)

    class MPTTMeta:
        order_insertion_by = ['name']



@receiver(post_save)
def translate_name(sender, instance, created, **kwargs):
    if sender not in [Category]:
        return
    if created:
        # some operations with 'name_ru' 'name_en' fields (since django-modeltranslation)
        instance.save(update_fields=['name'])

引发“一个节点不能成为其任何后代的子节点”

node        <Category: Obj3>
right       3L
target      <Category: Obj2>
level       1L
self        <mptt.managers.TreeManager object at 0x108a76d10>
width       2L
new_tree_id 2L
tree_id     2L
position    u'last-child'
left        2L

当我从这个 post_save 处理程序中排除 Category 模型时 - 一切正常

Django==1.8.2
django-mptt==0.7.4

任何想法或解决方法...

【问题讨论】:

    标签: django django-mptt


    【解决方案1】:
     def move_to(self, target, position='first-child'):
        """
        Convenience method for calling ``TreeManager.move_node`` with this
        model instance.
        NOTE: This is a low-level method; it does NOT respect
        ``MPTTMeta.order_insertion_by``.
        In most cases you should just move the node yourself by setting node.parent.
        """
    
    if instance.parent:
        instance.move_to(instance.parent)
    instance.save(update_fields=updated_field_list)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 2020-07-25
      相关资源
      最近更新 更多