How to add django-mptt rebuild to migration?

点点圈 提交于 2019-12-12 12:44:09

问题


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

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?


回答1:


Try the following:

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
      )
  ]


来源:https://stackoverflow.com/questions/49160623/how-to-add-django-mptt-rebuild-to-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!