Re: sorting a list of model instances
Hi, > Although I would have to wonder why you're doing it this way, rather > than using the database to sort them? Since in my case it is easier. I need to filter out some item before. This calculation needs to be in python code. Thomas -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: sorting a list of model instances
On Nov 7, 1:54 pm, Thomas Guettler <[EMAIL PROTECTED]> wrote: > Hi, > > I just discovered, that you can't sort a list of model instances. > > This failed with AssertionError: > > view_groups=sorted([brg.group for brg in ... ]) > change_groups=sorted([...]) > if view_groups==change_groups: > ... > else: > assert set(view_groups)!=set(change_groups), (view_groups, > change_groups) > > Is there a reason why this is this way? > > Thomas > > -- > Thomas Guettler,http://www.thomas-guettler.de/ > E-Mail: guettli (*) thomas-guettler + de You haven't given any key or cmp values in the sorted() call to tell it what to sort on. Certainly this works for me: sorted([item for item in qs], key=lambda x: x.name) Although I would have to wonder why you're doing it this way, rather than using the database to sort them? I understand that you're getting all group items that are related to a brg, but a better way of doing it would be something like: Group.objects.filter(brg__isnull=False).order_by('name') or whatever. -- DR. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: sorting a list of model instances
Wiadomość napisana w dniu 2008-11-07, o godz. 14:54, przez Thomas Guettler: > I just discovered, that you can't sort a list of model instances. > > This failed with AssertionError: > >view_groups=sorted([brg.group for brg in ... ]) >change_groups=sorted([...]) >if view_groups==change_groups: >... >else: >assert set(view_groups)!=set(change_groups), (view_groups, > change_groups) I always sort lists of model instances using sort(key=operator.attrgetter...) idiom (this sorts item in place and does not return a copy) and never saw such problems. -- We read Knuth so you don't have to. - Tim Peters Jarek Zgoda, R, Redefine [EMAIL PROTECTED] --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---