I have two models, model1 and model2, where model1 has a ForeignKey
reference to model2.

Given a list of model1 objects, I need to determine which of these are
used as a foreign key by a model2 object, and return that list. This
is my current effort:

returnList=[]
for model1 in modelList:
        model2List=model1.model2_set.distinct()
        if len(model2List) !=0:
            returnList.append(model1)
return returnList

There are quite many model2 objects in my database, so this approach
is rather slow. I'm guessing there is a more clever way to solve my
problem, but I'm quite new to both django and python. Can anybody
please give me a hint of a better solution?

Thanks!

Odd-R.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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