Hello

To understand the problem, assume I have a model consisting of Country
and SoccerTeam.

The SoccerTeam has a country = models.ForeignKey(Country).

Say, I need to find the countries having one or more related soccer
teams. I was not able to come up with a solution using the ORM, but got
to this using a list comprehension:

clist = [c for c in Country.objects.order_by('name') if
len(c.soccerteam_set.all()) > 0]

This is slow as hell. Understandable, as I have 213 countries and 4161
soccer teams in my test data.

I believe this could be done quite a lot faster using SQL.

Do I really need to use custom SQL here, or does the django ORM provide
the means to solve this problem?

Thanks,

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  |     Visit us at: http://www.gmta.info

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to