On 29 Jan, 09:52, Odd <o...@her-e-me.com> wrote: > On 29 Jan, 09:08, Odd <o...@her-e-me.com> wrote: > > > >You originally say the model1 > > > has a foreign key to model2 - I interpret that to mean that the > > > ForeignKey is defined on model1. However, your code references > > > model1.model2_set - which would imply that the FK is defined on > > > model2, pointing to model1. So, do you want the objects that are > > > pointed to by FKs from objects in the initial list, or the ones that > > > FKs in the initial list point to? > > > > return_list = Model1.objects.filter(model2__in=model2_list) > > > Sorry, the ForeignKey is defined on model2. I need to get all the > > model1 objects that is in the given modelList and is used as a > > foreignkey in a model2 object. Your suggestion assumes that the input > > is a model2_list, but the modelList is model1 objects. > > Figured it out. This seems to be the solution: > > return_list=Model1.objects.filter(model2__in modelList).distinct() >
Hmm... After a little testing, this is not what I wanted, some of the model1 objects in the return_list was not in the provided modelList, and I dont't quite understand why they are returned. As stated before, I want to get a distinct list of all model1 objects that is in a provided list and is used as a foreignkey in any model2 object. Is this doable? Thanks. -- 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.