Hi -
I have these models; with one custom manager.
class OrderByHighestScoreManager(models.Manager):
def get_query_set(self, *args, **kwargs):
qs = super(OrderByHighestValidationsManager,
self).get_query_set(*args, **kwargs)
return
qs.annotate(score=Count('votes__id'),).order_by('score')
class AbstractEntry(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
last_modified = models.DateTimeField(auto_now=True)
objects = models.Manager()
order_by_votes = OrderByHighestValidationsManager()
class Entry(AbstractEntry):
creation_date = models.DateTimeField(auto_now_add=True)
votes = generic.GenericRelation(Vote)
================================================
I can't get the custom manager to work....
This is OK:
Entry.objects.annotate(score=Count('votes__id'),).order_by('score')
This does not work:
Entry.order_by_votes.all()
The error I get is:
Error
Traceback (most recent call last):
File "c:\Python27\Lib\unittest\case.py", line 318, in run
testMethod()
File "C:\Data\Development\django_projects\oko\________\apps\entries
\tests\model.py", line 111, in test_order_by_votes
self.assertEqual(list(Entry.order_by_votes.values_list('id',
flat=True)), [self.e1.id, self.e2.id])
File "C:\Data\Development\django_projects\oko\lib\site-packages
\django\db\models\query.py", line 84, in __len__
self._result_cache.extend(self._iter)
File "C:\Data\Development\django_projects\oko\lib\site-packages
\django\db\models\query.py", line 956, in iterator
for row in self.query.get_compiler(self.db).results_iter():
File "C:\Data\Development\django_projects\oko\lib\site-packages
\django\db\models\sql\compiler.py", line 680, in results_iter
for rows in self.execute_sql(MULTI):
File "C:\Data\Development\django_projects\oko\lib\site-packages
\django\db\models\sql\compiler.py", line 725, in execute_sql
sql, params = self.as_sql()
File "C:\Data\Development\django_projects\oko\lib\site-packages
\django\db\models\sql\compiler.py", line 60, in as_sql
ordering, ordering_group_by = self.get_ordering()
File "C:\Data\Development\django_projects\oko\lib\site-packages
\django\db\models\sql\compiler.py", line 349, in get_ordering
self.query.model._meta, default_order=asc):
File "C:\Data\Development\django_projects\oko\lib\site-packages
\django\db\models\sql\compiler.py", line 378, in find_ordering_name
opts, alias, False)
File "C:\Data\Development\django_projects\oko\lib\site-packages
\django\db\models\sql\query.py", line 1238, in setup_joins
"Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'score' into field. Choices are:
creation_date, id, last_modified, user, votes
=================================================================
What's going wrong here?
I would very much like to perform this sorting via a manager, or at
least a method accessible from within an Entry instance as several
templates will be using this special sorting (and I don't want to copy
this complex queryset over different views).
Thanks in advance!
--
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?hl=en.