1

I need to use categories in django server, and I am currently using mptt and mptt-admin. That is what I am trying to do:

#### models.py ####

class File(MPTTModel)
name = models.CharField(max_length=20)
description = models.CharField(max_length=500)
....
parent = mptt.fields.TreeForeignKey('Category', blank=False, null=False, related_name='children', db_index=True)


class Category(MPTTModel):
    name = models.CharField('Category', max_length=20, blank=False, unique=True)
    parent = mptt.fields.TreeForeignKey('self', blank=True, null=True)

Both have been registered:

mptt.register(Category,)
mptt.register(File, parent = Category)

admin.py

class FileAdmin(admin.ModelAdmin):
    fieldsets = [
    ...
    ]
    list_display = ('name',)

class CategoryAdmin(DjangoMpttAdmin):
    tree_title_field = 'name'
    tree_display = ('name',)


admin.site.register(File, FileAdmin)
admin.site.register(Category, CategoryAdmin)

I also tried replacing TreeForeignKey with ForeignKey and MPTTModel with models.Model, but I got the same result.

In mysite/admin, I can add categories and files. Every file has a category. While deleting a category, all related files are also deleted, but how can I show categories with all child files through mptt-admin?

By the way, when I create the parent in the same class, all child files has shown.

Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
Andrew
  • 161
  • 2
  • 14
  • Unrelated note: I don't think you have to use `mptt.register` when you're subclassing `MPTTModel` with your models. See http://django-mptt.github.io/django-mptt/models.html#registration-of-existing-models – Dunya Degirmenci Mar 28 '14 at 13:09

0 Answers0