hello #django. Guys, I'm having some difficulties to understand how can I select all objects in a ManyToMany field that are set on other model. Here is the models.pyhttps://dpaste.de/OwkbQ/
you can ask the ORM to fetch the target of that select_related in the same query, instead of waiting for you to access it
RileyII: s/would/could/
hyperair has quit
mmello
FunkyBob: the problem is that I'll have to do a loop for all Org objects and then get the org.instructors.all()
RileyII
Could, yes.
FunkyBob
mmello: well, what is it you actually want?
oh, you want all the Person instances that are related to _any_ Org?
CtrL_X_ has quit
mmello
I want to get all Persons in any Org
yes
is there a way to avoid the loop in this case?
FunkyBob
Person.objects.filter(org_set__isnull=False) # org_set? or org? ...never seem to remember
mmello
hm.. lemme see
not worked :( that is my doubt
FunkyBob
don't just say "not worked"
what happened?
what did you use?
we can't help you fix it if you don't tell us _how_ it went wrong
glassresistor has quit
mmello
sure thing. lemme show
FunkyBob: it says that org_set is not field
FunkyBob
so use org
org__isnull=False
kamol has quit
mmello
Person.objects.filter(org__isnull=False)
worked
darwin_bio has left the channel
but, I got all Person objects
skeet70 has quit
kamol joined the channel
and not only the objects that are set on m2m field on Org
dencold has quit
RileyII
mmello: So you're trying to load all of the Person instances that have more than zero associated Org instances? Is that it?
batisteo joined the channel
FunkyBob
mmello: thats what I asked if you wanted
(00:00) < FunkyBob> oh, you want all the Person instances that are related to _any_ Org?
mmello
RileyII: I want to get all the Person instances that are also set on Org.instructors as well
paiti joined the channel
paiti has quit
FunkyBob
you have the set of Person objects which have a relation through that m2m to at least one Org
ehmatthes joined the channel
the_rat_ has quit
QuanSai joined the channel
QuanSai
ahoy
the_rat joined the channel
foundatron has quit
FunkyBob
morning, QuanSai
hell_razer joined the channel
mmello
FunkyBob: some Org instances does not contains any instructors yet. but other Org do. So, I could get all those Person objects by doing a loop on Org and putting in a list the Org.instructors.all(). But I was wondering how I can do that with the ORM
RileyII
So Person.objects.filter(org__in=Org.objects.all()) then? (This solution isn't very efficient, and there may be a better way.)
FunkyBob
mmello: I don't understand what's wrong with my solution
mmello
RileyII: this works, but it brings me all the Person objects and not only the instructors
FunkyBob
you want the list of Persons associated with any Org?
mmello
FunkyBob: yes
FunkyBob
well, that's what I gave you
Laybunz joined the channel
mmello
I have 91 objects on Person() class ok?
FunkyBob
you probably want a distinct() on it too
ambar has quit
mmello
and just 2 of those Person() are present on Org.instructor
so, I want only that 2 persons be listed
FunkyBob
ok
and how many did my suggested query yield?
mmello
91
the_rat has quit
ionelmc has quit
jjmalina joined the channel
FunkyBob
is there any other relation between Person and Org ?
for some reason, my template isn't being accessed. The path is correct. Not sure why Django is reporting that the template doesn't exist. What should I paste to show you guys what I'm doing?