Django mptt, category and subcategory system - Category has no field named 'parent'

I would like created category and subcategory system using django mptt. My try:

from django.db import models
import mptt


class Category(models.Model):
    name = models.CharField(max_length=255)
    category = models.ForeignKey('self', null=True, blank=True, related_name='children')

mptt.register(Category, order_insertion_by=['name'])

class Post(models.Model):
    title = models.CharField(max_length=255)
    text = models.TextField()
    category = models.ManyToManyField(Category)

My admin.py

from django.contrib import admin
from xxxx.xxx.models import *
from mptt.admin import MPTTModelAdmin

admin.site.register(Category, MPTTModelAdmin)
admin.site.register(Post)

My error:

Category has no field named 'parent'

Answers


Django-mptt requires field with name parent with ForeignKey('self')

http://django-mptt.github.com/django-mptt/models.html#setting-up-a-django-model-for-mptt

You can overwrite that with:

class Category(models.Model):
    name = models.CharField(max_length=255)
    category = models.ForeignKey('self', null=True, blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by=['name']
        parent_attr = 'category'

Need Your Help

Emacs - Toggling between Visual Markers?

emacs elisp bookmarks

I'm trying to figure out if this functionality exists, or if not how difficult it would be to program.

Sandbox account posted to live gamecenter boards! Any way to delete score?

xcode game-center

I was testing out an update to my app, and while logged into my gamecenter sandbox account on the device, ran my project on my iPhone. Foolishly, without thinking to check I was in sandbox mode, I ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.