开发者

Ordering django-mptt foreign key in admin not working

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 Docu开发者_开发知识库ment(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',)


OK - found the answers with some persistance:

Why is display list not ordered correctly? Because it only uses the first field:

ModelAdmin.ordering Set ordering to specify how lists of objects should be ordered in the Django admin views. This should be a list or tuple in the same format as a model's ordering parameter.

If this isn't provided, the Django admin will use the model's default ordering.

Note Django will only honor the first element in the list/tuple; any others will be ignored.

Why was the select drop down not ordered correctly? Because I had to have an order in the the subclass, not just the abstract model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜