如何将django-mptt重建添加到迁移中? [英] How to add django-mptt rebuild to migration?

查看:209
本文介绍了如何将django-mptt重建添加到迁移中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将django-mptt添加到现有数据库中,并创建了新的迁移.

I have add the django-mptt to existing database, and create the new migration.

要求迁移过程提供levelleftright等字段的默认值,但不会将model.rebuild操作添加到迁移文件中.

Migration process was asked for default values for level, left, right and such fields, but doesn't add the model.rebuild operation to migration file.

如何在迁移文件中手动添加重建操作?

How to add rebuild operation to migration file manually?

推荐答案

尝试以下操作:

from __future__ import unicode_literals
from django.db import migrations
from mptt import register, managers


def rebuild_tree(apps, schema_editor):
    YourMPTTModel = apps.get_model('your_app', 'YourMPTTModel')

    manager = managers.TreeManager()
    manager.model = YourMPTTModel

    register(YourMPTTModel)

    manager.contribute_to_class(YourMPTTModel, 'objects')
    manager.rebuild()


class Migration(migrations.Migration):


  operations = [
      migrations.RunPython(
          rebuild_tree
      )
  ]

这篇关于如何将django-mptt重建添加到迁移中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆