我无法在django admin中创建对象。它引发了错误:
ValueError at /admin/app/category/add/
"<Category: >" needs to have a value for field "from_category" before this many-to-many relationship can be used.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/app/category/add/
Django Version: 1.6.5
Exception Type: ValueError
Exception Value:
"<Category: >" needs to have a value for field "from_category" before this many-to-many relationship can be used.
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py in __init__, line 524
Python Executable: /usr/bin/python
Python Version: 2.7.6
我无法理解出了什么问题。我有以下models.py:
from django.db import models
from mptt.models import MPTTModel, TreeManyToManyField
class Category(MPTTModel):
name = models.CharField(max_length=255, default=u'')
engName = models.CharField(max_length=255, default=u'', blank=True)
parents = TreeManyToManyField('self', symmetrical=False, related_name='children', blank=True)
description = models.TextField(default=u'', blank=True)
active = models.BooleanField(default=True, blank=True)
def __unicode__(self):
return u"{}".format(self.name)
class MPTTMeta:
order_insertion_by = ['name']
parent_attr = 'parents'
和admin.py:
from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from app.models import Category, Pattern
admin.site.register(Category, MPTTModelAdmin)
admin.site.register(Pattern)
答案 0 :(得分:3)
问题在于TreeManyToManyField
父母。
在树结构中,节点只能有一个父节点。所以mptt不支持这个。
如果您使用TreeForeignKey
,那么您应该有更好的运气。
我添加了一个note to the django-mptt docs。