Issue #73 open

sort models?

patrickk
created an issue

this is more a question than an actual issue ... any reasons why the modellist is not sorted? because the models within an applist are sorted by title.

IMO it´s good to sort the models (within modellist) manually and independent from the title ... but as a default, sorting by title could be useful.

what do you think?

Comments (9)

  1. David Jean Louis repo owner

    No good reason. I think that something like this should ensure that models are always sorted (in menus and dashboard modules):

    --- a/admin_tools/dashboard/modules.py	Tue Feb 22 10:57:50 2011 +0100
    +++ b/admin_tools/dashboard/modules.py	Wed Feb 23 12:12:41 2011 +0100
    @@ -435,8 +435,6 @@
             apps_sorted = apps.keys()
             apps_sorted.sort()
             for app in apps_sorted:
    -            # sort model list alphabetically
    -            apps[app]['models'].sort(lambda x, y: cmp(x['title'], y['title']))
                 self.children.append(apps[app])
             self._initialized = True
     
    diff -r 3ccc71925515 admin_tools/utils.py
    --- a/admin_tools/utils.py	Tue Feb 22 10:57:50 2011 +0100
    +++ b/admin_tools/utils.py	Wed Feb 23 12:12:41 2011 +0100
    @@ -87,6 +87,10 @@
                 model, perms = item
                 if fnmatch(full_name(model), pattern):
                     result.remove(item)
    +    result.sort(lambda x, y: cmp(
    +        x[0]._meta.verbose_name_plural,
    +        y[0]._meta.verbose_name_plural
    +    ))
         return result
    

    It would be great if you could confirm this by applying the diff, because I currently don't have access to a test app.

    -- David

  2. patrickk reporter

    yes, but I think that models should not _always_ be sorted (by name/title). because sometimes (actually in most cases) I´m listing models invididually within a modellist. and I´m not sure how to distinguish between a list which should be sorted by title (or maybe any other attribute) or a list which should be sorted according to the position of models within the modellist ... I hope you know what I mean (bit hard to describe).

    the main issue with this ticket was to ask if you thought about this and if there´s any reason why the models are not sorted (by default).

    I´ll do some more research and I´ll hopefully be back with a decent solution.

  3. defaultwombat

    By changing the filter_models function models are sorted with respect to the order of the ModelList.models list. Models added with wildcards will be sorted among each other before the get added to the ModelList.children. This should give a predictable and stable order with the possibility to change it.

    --- a/admin_tools/utils.py	Wed Feb 23 14:41:35 2011 +0100
    +++ b/admin_tools/utils.py	Wed Mar 09 08:29:59 2011 +0100
    @@ -76,11 +76,14 @@
             included = items
         else:
             for pattern in models:
    +            pattern_items = []
                 for item in items:
                     model, perms = item
                     if fnmatch(full_name(model), pattern) and item not in included:
    -                    included.append(item)
    -
    +                    pattern_items.append(item)
    +            pattern_items.sort(key=lambda x:x[0]._meta.verbose_name_plural)
    +            included.extend(pattern_items)
    +    
         result = included[:]
         for pattern in exclude:
             for item in included:
    
    

    Example:

    self.children.append(modules.ModelList(
        u'',
        models=('app1.models.ZModel','app2.models.*','app3.models.AModel'),
    ))
    # ==> ['app1.ZModel','app2.BModel','app2.CModel','app3.AModel']
    
  4. hirunatan

    I have tried the patch of defaultwombat in the repository version, and it works, but needs one more thing. The filter_models function now returns the models correctly ordered, but there is another function later that sorts them again alphabetically. It's enough to remove this line, and everything works as expected.

    Here is the updated patch:

    --- a/admin_tools/utils.py	Wed Feb 23 14:41:35 2011 +0100
    +++ b/admin_tools/utils.py	Wed Mar 09 08:29:59 2011 +0100
    @@ -76,11 +76,14 @@
             included = items
         else:
             for pattern in models:
    +            pattern_items = []
                 for item in items:
                     model, perms = item
                     if fnmatch(full_name(model), pattern) and item not in included:
    -                    included.append(item)
    -
    +                    pattern_items.append(item)
    +            pattern_items.sort(key=lambda x:x[0]._meta.verbose_name_plural)
    +            included.extend(pattern_items)
    +    
         result = included[:]
         for pattern in exclude:
             for item in included:
    
    --- a/admin_tools/dashboard/modules.py	2011-07-05 12:33:08.327692370 +0200
    +++ b/admin_tools/dashboard/modules.py	2011-07-05 12:33:12.967699180 +0200
    @@ -435,8 +435,6 @@
             apps_sorted = apps.keys()
             apps_sorted.sort()
             for app in apps_sorted:
    -            # sort model list alphabetically
    -            apps[app]['models'].sort(lambda x, y: cmp(x['title'], y['title']))
                 self.children.append(apps[app])
             self._initialized = True
    
  5. jboonstra

    Recently installed django-admin-tools 0.4.1 using Django 1.3.1. Love it. Just need my models sorted alphabettically on the ChangeList screens. Tried installing patch provided by hirunatan, and received the following error:

    File to patch: utils.py
    patching file utils.py
    patch unexpectedly ends in middle of line
    Hunk #1 FAILED at 435.
    1 out of 1 hunk FAILED -- saving rejects to file utils.py.rej
    
    

    Any advice would be most appreciated. Thanks!

  6. Log in to comment