Waylan Limberg wrote:
> Actually, as you have `related_name` set (to 'stats'), I believe this
> may be what you want:
> 
> Book.objects.all().select_related().order_by('-stats__avg_rating')[0:10]

OK, as this failed as well, I made a clean example, from ZERO, and tried 
it. Any ideas why this would fail would be great ;-)

Here goes:

models.py
---------

from django.db import models

class Book(models.Model):
        title = models.TextField()

class BookStat(models.Model):
        book = models.OneToOneField(Book, related_name='stats')
        times_read = models.PositiveIntegerField()


After syncing the DB, in the command line:
------------------------------------------

(
        saving 1 book + stat, then trying:
        Book.objects.all().order_by("stats__times_read")
)

 >>> from orderby.example.models import *
 >>> book = Book.objects.create(title="BFG")
 >>> book.save()
 >>> bs = BookStat.objects.create(book=book, times_read=15)
 >>> bs.save()
 >>> Book.objects.all().order_by("stats__times_read")
Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "C:\Python25\lib\site-packages\django\db\models\query.py", line 
101, in __repr__
     return repr(self._get_data())
   File "C:\Python25\lib\site-packages\django\db\models\query.py", line 
444, in _get_data
     self._result_cache = list(self.iterator())
   File "C:\Python25\lib\site-packages\django\db\models\query.py", line 
181, in iterator
     cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + 
",".join(select) + sql, params)
   File "C:\Python25\lib\site-packages\django\db\backends\util.py", line 
12, in execute
     return self.cursor.execute(sql, params)
   File 
"C:\Python25\lib\site-packages\django\db\backends\postgresql\base.py", 
line 43, in execute
     return self.cursor.execute(sql, [smart_basestring(p, self.charset) 
for p in params])
ProgrammingError: ERROR:  column example_book.stats__times_read does not 
exist at character 80
SELECT "example_book"."id","example_book"."title" FROM "example_book" 
ORDER BY "example_book"."stats__times_read" ASC

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

Reply via email to