On 10 Oct 2006, at 15:08, Tom Smith wrote:
>
> Sorry for what is probably a simple python question, but if I have a
> list of Products and I have searched for 100 of them ordered by x...
>
> then how do I then sort the QuerySet returned by title?
>
> I can't do an .. object_list = products.items().sort('title')
>
> because I have already done a [:100] on the results...
... answering my own question but this works... probably will be slow
for a large collection of objects but it's useful for making a
tagcloud (unfinished)..
def tagcloud(request):
def sort(objects, sortAttrib):
sortValues = [(getattr(objects[i], sortAttrib), i) for i in
range
(len(objects))]
sortValues.sort(), # Sorts by first value in tuple
return [objects[sortTuple[1]] for sortTuple in sortValues]
words = Word.objects.all().order_by('value').order_by('-
product_count')[:100] # I have calculated these before-hand by
splitting product.name into words and creating 2 tables (word and
product_word)
big_num = words[0].product_count # get the biggest
for word in words:
word.percentage = ((word.product_count * 100) / big_num) + 10
#
the 10 is a hack so the fonts don't disappear....
words = sort(words, 'value') #see above
return render_to_response('tagcloud.html', add_defaults
( {'object_list':words} ) )
{% for word in object_list %}
<font style="font-size:{{word.percentage}}px;vertical-
align:middle;"><a href="{{BASE_URL}}words/{{word.value}}/"
title="{{word.product_count}} items">{{word.value}}</a></
font>
{% endfor %}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---