Am using django-mptt to create a Categories model and then using that as a foreign key for a Documents model. The Categories admin works fine and categories are displayed in tree order as expected. However I have two problems with ordering for the Document model in admin.
The Documents in the admin list are being listed in the id order not category order The drop down list for Category in the edit screen is listed in category id order. Note that I was using an abstract class for category for another reason.
Why is the order I have specified in the model being ignored?
Models.py
class Category(MPTTModel):
parent = models.ForeignKey('self', related_name="children")
name = models.CharField(max_length=100)
class Meta:
abstract = True
ordering = ('tree_id', 'lft')
class MPTTMeta:
ordering = ('tree_id', 'lft')
order_insertion_by = ['name',]
class CategoryAll(Category):
class Meta:
verbose_name = 'Category for Documents'
verbose_name_plural = 'Categories for Documents'
class Document(models.Model):
title = models.CharField(max_length=200)
file = models.FileField(upload_to='uploads/library/all', blank=True, null=True)
category = models.ForeignKey(CategoryAll)
class Meta:
ordering = ('category__tree_id', 'category__lft', 'title')
Admin.py
class DocAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'category')
list_filter = ('author','category')
ordering = ('category__tree_id', 'category__lft', 'title')
UPDATE FIXED:
Models.py
class Category(MPTTModel):
parent = models.ForeignKey('self', related_name="children")
name = models.CharField(max_length=100)
class Meta:
abstract = True
class MPTTMeta:
order_insertion_by = ['name',]
class CategoryAll(Category):
class Meta:
verbose_name = 'Category for Documents'
verbose_name_plural = 'Categories for Documents'
ordering = ('lft',)
class Document(models.Model):
title = models.CharField(max_length=200)
file = models.FileField(upload_to='uploads/library/all', blank=True, null=True)
category = models.ForeignKey(CategoryAll)
class Meta:
ordering = ('category__tree_id', 'category__lft', 'title')
Admin.py
class DocAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'category')
list_filter = ('author','category')
ordering = ('category__lft',)