如何在创建Django夹具时填充继承的MPTT字段?

时间:2014-10-21 04:24:03

标签: python django django-mptt

我按照here的说明创建了我的第一个Django灯具。我创建了一个JSON文件。但是有一个复杂的问题:我试图填充MPTTModel

的继承模型

这是我的Django JSON fixture文件initial_data.json的样子:

[
    {
         "model": "MyApp.MyModel", 
         "pk": 1, 
         "fields": {
             "level": 0, 
             "parent": null, 
             "name": "My String"
         }
    }
]

当我运行此灯具时,我收到如下所示的错误。事实证明,必须在此模型的灯具中定义一些其他字段:rghtlfttree_idlevel

django.db.utils.IntegrityError: Problem installing fixture    
'MyApp/fixtures/initial_data.json': Could not load MyApp.MyModel(pk=1): 
    null value in column "lft" violates not-null constraint
Failing row contains (1, My String, null, , null, null, null, 0).

我可以弄清楚level属性应该是什么,并将其插入到fixture文件中。但是我该如何在这个灯具中找出并设定rghtlfttree_id的值?

1 个答案:

答案 0 :(得分:2)

你是如何手动创建灯具的?使用dumpdata命令可以清楚地看到所需的结构,这里是一个继承模型json转储的例子:

$ ./manage.py dumpdata cms.Page --natural --indent=4
[
    {
        "pk": 46,
        "model": "cms.page",
        "fields": {
            "rght": 43,
            "navigation_extenders": "",
            "site": 1,
            "creation_date": "2014-05-31T12:07:12Z",
            "lft": 42,
            "in_navigation": false,
            "reverse_id": null,
            "login_required": false,
            "created_by": "hedde",
            "publication_end_date": null,
            "template": "cms/page.html",
            "tree_id": 2,
            "placeholders": [
                159,
                160
            ],
            "changed_date": "2014-06-25T09:33:50Z",
            "limit_visibility_in_menu": null,
            "parent": 2,
            "publisher_state": 0,
            "soft_root": false,
            "publication_date": "2014-05-31T12:07:07Z",
            "publisher_public": 45,
            "level": 1,
            "changed_by": "hedde",
            "publisher_is_draft": false,
            "published": true
        }
    }
]