The problem with the sorting method is that I think it will take to much
time. I would like if it could happen as part of the query.

"operator module to get attributes of the object(s) in your QuerySet", I'm
not sure I know how to do this.


I have tried writing it as SQL with cursor:
cursor.execute("SELECT word, ref_id, COUNT(ref_id) c FROM search_searchword
WHERE word LIKE %s OR word=%s GROUP BY ref_id ORDER BY c desc",["%est%",
"test2sf"])  #"SELECT foo FROM bar WHERE baz = %s", [self.word])

It works byt then I have the problem that ref_id is a foreign key and I
would like to have the objects i points to a long as well.

It looks like it is by far easier to find some way to it in Django



Brandon Taylor wrote:
> 
> 
> Hello,
> 
> You can use the sorted() function in Python to order any iterable. It
> asks for a key to order by, which can be a lambda, or you could use
> the operator module to get attributes of the object(s) in your
> QuerySet object to provide the key.
> 
> My $0.02,
> Brandon
> 
> On Jan 29, 12:38 pm, lollerikken <lollerik...@gmail.com> wrote:
>> Hello, sorry about my English, but I hope that it is understandable
>>
>>   I am having some trouble using order_by with Django.
>>
>>   I think it would be easier to explain what I want with an example:
>>
>>   fx if I have this table:
>>
>> key ref
>> 1  a
>> 2  g
>> 3  a
>> 4  c
>> 5  g
>> 6  g
>>
>> I would like to sort it like this:
>>
>> key ref
>> 2  g
>> 5  g
>> 6  g
>> 1  a
>> 3  a
>> 4  c
>>
>> because there is the highest count of g's etc.
>>
>> My google search suggested that I should use something called annotate
>> combined with count, but it does not work.
>> I have tried something like:
>> table.objects.values('key',
>> 'ref').annotate(nr_ref=Count('ref')).order_by('nr_ref')
>> but without any luck.
>> --
>> View this message in
>> context:http://www.nabble.com/order-by-count-tp21732508p21732508.html
>> Sent from the django-users mailing list archive at Nabble.com.
> > 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/order-by-count-tp21732508p21738150.html
Sent from the django-users mailing list archive at Nabble.com.


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to