i believe you can do this with "extra" attribute.

go to http://docs.djangoproject.com/en/1.3/ref/models/querysets/ and
search for subquery. you'll see how to do subqueries.

On Apr 6, 5:30 pm, bernatbonet <bernatbo...@gmail.com> wrote:
> Data Model:
> class A(models.model):
>    desc: model.CharField()
> class B(models.model):
>    a: model.ForeignKey('A')
>    desc: models.CharField()
> I need to do this select in a view:
>    select max(num_a) as max_num_a from (select a, count(desc) as
> num_a
> from B group by a) as x;
> I've tried this:
>    result =
> B.objects.values('a').annotate(num_a=Count('a')).aggregate(Max('num_a'))
> And I got this error:
>    1064, "You have an error in your SQL syntax; check the manual that
> corresponds to your MySQL server version for the right syntax to use
> near 'FROM (SELECT `B`.a` AS `a`, COUNT(`a' at line 1")
> If we only group and not obtain max it works:
>    result = B.objects.values('a').annotate(num_a=Count('a'))
> and the result is : {'a': 1L, 'num_a': 9}{'a': 2L, 'num_a': 6}{'a':
> 3L,
> 'num_a': 4}
> I'm not sure what I'm doing wrong, neither if there's other way to
> resolve it.
> If somebody have been faced with and resolved it, I'll be gratefull if
> he can show me how can I start for beating this.
> Thanks all.

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