Re: best practice to have a list of mixed objects
thanks I didn't know about that one, I'll give it a try! On Jul 2, 12:57 pm, Jarek Zgoda wrote: > Wiadomość napisana w dniu 2009-07-02, o godz. 12:45, przez Bastien: > > > I am trying to have a list of various types of objects, for example a > > list mixed with users' profile, users' photo... to try to order all > > these elements together by the amount of votes they received. > > > I use the list() function to store each query in a different variable > > and then I extend() the list in only one variable. > > > I'm sure there's a better way to do that, if you have any idea you're > > welcome to share it! > > If all objects have same named attribute for votes (i.e. > profile.votes, photo.votes) you may use attrgetter() to sort ordinal > Python list, eg.: > > import operator > my_objects.sort(key=operator.attrgetter('votes')) > > -- > Artificial intelligence stands no chance against natural stupidity > > Jarek Zgoda, R&D, Redefine > jarek.zg...@redefine.pl --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: best practice to have a list of mixed objects
Wiadomość napisana w dniu 2009-07-02, o godz. 12:45, przez Bastien: > I am trying to have a list of various types of objects, for example a > list mixed with users' profile, users' photo... to try to order all > these elements together by the amount of votes they received. > > I use the list() function to store each query in a different variable > and then I extend() the list in only one variable. > > I'm sure there's a better way to do that, if you have any idea you're > welcome to share it! If all objects have same named attribute for votes (i.e. profile.votes, photo.votes) you may use attrgetter() to sort ordinal Python list, eg.: import operator my_objects.sort(key=operator.attrgetter('votes')) -- Artificial intelligence stands no chance against natural stupidity Jarek Zgoda, R&D, Redefine jarek.zg...@redefine.pl --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
best practice to have a list of mixed objects
Hello, I am trying to have a list of various types of objects, for example a list mixed with users' profile, users' photo... to try to order all these elements together by the amount of votes they received. I use the list() function to store each query in a different variable and then I extend() the list in only one variable. I'm sure there's a better way to do that, if you have any idea you're welcome to share it! Thanks, Bastien --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---