Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#7942 closed (fixed)

Admin doc does not reflect the fact that In multi-column ordering, only the first column is used.

Reported by: Martin Diers Owned by: MattBowen
Component: Documentation Version: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When setting the ordering property in the ModelAdmin class for a model, to a multiple column tuple, the resulting order is not correct.

In my case, I am working with an MPTT tree, such that I have tree_id, level, and lft properties, which I am using to sort.

Here is my ModelAdmin class:

class PageOptions(ModelAdmin):
    list_display = ('name', 'page_url', 'description','tree_id', 'level', 'lft')
    ordering = ('tree_id','level','lft',)
    fieldsets = (
        ('Content', {'fields': ('name','title','description','page_url','content','template','menu_text', 'other_menu_text')}),
        ('MPTT', {'fields': ('lft','rght','tree_id','level'), 'classes': ('collapse','wide')}),
    )

Here is my Model:

class Page(models.Model):
    name = models.CharField(max_length=25)
    title = models.CharField(max_length=80,null=True)
    description = models.TextField(null=True)
    menu_text = models.CharField(max_length=25, null=True, blank=True, help_text="Leave blank to exclude from menu.")
    other_menu_text = models.CharField(max_length=25, null=True, blank=True, help_text="Assigns this page to an alternate menu. Leave blank to exclude from menu.")
    page_url = models.CharField(max_length=20, unique=True)
    template = models.ForeignKey(Template, limit_choices_to = {'page_template': True})
    content = models.TextField()
    # The following fields are for implementing MPTT
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
    lft = models.PositiveIntegerField(null=True, blank=True)
    rght = models.PositiveIntegerField(null=True, blank=True)
    tree_id = models.PositiveIntegerField(null=True, blank=True)
    level = models.PositiveIntegerField(null=True, blank=True)    
    
    class Meta:
        ordering = ('tree_id','level','lft',)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        path = list(self.get_ancestors().values_list('page_url', flat=True))
        path.append(self.page_url)
        return "/" + "/".join(path)        

And this is the sort I am getting:

tree level lft
  2    0    1
  3    1    4
  3    0    1
  3    1    6
  3    1    2
  4    0    1  

As you can see, it is sorting on the first column, and ignoring the other two. I tried this with and without the ordering property in the model's Meta class.
This may be related to #7309.

Attachments (1)

admin-ordering-7942.diff (589 bytes ) - added by MattBowen 16 years ago.
Added note that admin only respects the first element in a list.

Download all attachments as: .zip

Change History (7)

comment:1 by Martin Diers, 16 years ago

Component: Admin interfaceDocumentation
Summary: Admin: In multi-column ordering, only the first column is used.Admin doc does not reflect the fact that In multi-column ordering, only the first column is used.

Looking at the ChangeList.get_ordering() function, it is clear that the admin is ignoring the other columns by design.

This is really a documentation problem, and I am changing the ticket to reflect that.

The admin.txt document states:

``ordering``
~~~~~~~~~~~~

Set ``ordering`` to specify how objects on the admin change list page should be
ordered. 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.

Obviously, there should be a note here stating that only the first column is used, and all others are ignored.

comment:2 by Brian Rosner, 16 years ago

milestone: 1.0 beta
Triage Stage: UnreviewedAccepted

It is correct that ChangeList only works with one field. There is another ticket open for that. One day it will support more than one. It will be a big change since it will involve UI changes as well. Marking as accepted for the docs change.

comment:3 by MattBowen, 16 years ago

Owner: changed from nobody to MattBowen
Status: newassigned

by MattBowen, 16 years ago

Attachment: admin-ordering-7942.diff added

Added note that admin only respects the first element in a list.

comment:4 by MattBowen, 16 years ago

Has patch: set

comment:5 by Brian Rosner, 16 years ago

Resolution: fixed
Status: assignedclosed

(In [8210]) Fixed #7942 -- Added a note to the admin ordering option documentation about the admin only respecting the first element. Thanks mwdiers for the report and MattBowen for the original patch.

comment:6 by Jacob, 13 years ago

milestone: 1.0 beta

Milestone 1.0 beta deleted

Note: See TracTickets for help on using tickets.
Back to Top