4

現在のプロジェクトで mptt を動作させようとしていますが、データベースの移行に問題があります。

ここに私のモデルがあります

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey

class Section(MPTTModel):
    name = models.CharField(max_length=50, unique=True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)

    class MPTTMeta:
        order_insertion_by = ['name']

コマンドラインでこれを実行しています:

sudo python manage.py makemigrations core

しかし、レベルフィールドに関連するエラーが発生しているようです

You are trying to add a non-nullable field 'level' to section without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option:

私は何をすべきか?

4

2 に答える 2

4

'Level' is automatically added by MPTTModel to denote the 'depth' of a particular node in the tree. If you haven't created the tree structure yet, it should be safe to choose option 1 and default everything to level 0 (root). If you do not have the tree structure set up yet, this should be fine and should get adjusted as you work with the tree later.

If you already have a tree structure in mind and you need to reflect that in your data, you will still need to do this, but you will need to follow it with a (probably handwritten) data migration to set the right values.

于 2015-05-26T21:33:25.477 に答える