I can't create an object in django admin. It raises the error:
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
I can't understand what's going wrong. I have the following 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'
and 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)
The problem with this is the TreeManyToManyField
parent.
In a tree structure, nodes can only have one parent. So mptt doesn't support this.
If you use a TreeForeignKey
instead you should have better luck.
I've added a note to the django-mptt docs about this.