Thanks a lot, guys. I'll probably have some more questions once I try
this out, but thanks so much!
-Dougal
On Mar 17, 2:45 pm, Jonathan Buchanan <[EMAIL PROTECTED]>
wrote:
> [EMAIL PROTECTED] wrote:
> > If I have a model with something like
>
> > class A (models.model):
> > parent = meta.ForeignKey('self', blank=True, null=True,
> > related_name='child')
>
> > Is it possible to do a select for some results in A along with a count
> > of all their children?
>
> > -Dougal
>
> You could use the extra method like this, which depends on the fact that
> you'll already be selecting from appname_a in the query generated by the
> ORM:
>
> A.objects.extra(
> select={
> 'child_count': 'SELECT COUNT(*) FROM appname_a children WHERE
> children.parent_id = appname_a.id',
> }
> )
>
> Personally, I'd put that in a custom Manager method
> (with_child_counts?), using the Model's metadata to fill in the table
> name information.
>
> Jonathan.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---