#django

/

      • merlin83_ joined the channel
      • luyikei__ has quit
      • epicowl has quit
      • ehmatthes has quit
      • thesheff17 joined the channel
      • mmello
        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/
      • hell_razer has quit
      • FunkyBob
        mymode.m2mfield.all()
      • mmello
        I think I did not understand select_related() yet
      • FunkyBob
        org.instructors.all()
      • select_related is an optimisation tool
      • mattmcc
        And it doesn't apply to M2Ms anyway.
      • mstrcnvs has left the channel
      • mmello
        oh.. gotcha.
      • in which case can I use select_related ?
      • FunkyBob
        when you have a fkey
      • RileyII
        You would use prefetch_related for your case.
      • mattmcc
        Maybe. It depends on the situation.
      • FunkyBob
        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 ?
      • jjmalina has quit
      • mmello
        len(Person.objects.filter(org__in=Org.objects.all()))
      • 91
      • FunkyBob
        that's not what I recommended
      • twoolie joined the channel
      • mmello
        len(Person.objects.filter(org__isnull=False))
      • 91
      • FunkyBob
        try exclude(org__isnull=True)
      • mmello
        is there any other relation between Person and Org ?
      • yes.. there is other field
      • on Org
      • FunkyBob
        .. what other field?
      • mmello
        on Org
      • tech = models.ForeignKey(Person)
      • tech = models.ForeignKey(Person, selected_related='tech')
      • FunkyBob
        and neither have a related_name ?
      • mmello
        yes.. both has
      • FunkyBob
        select_realted? you can't ut that on a fkey
      • dirn joined the channel
      • mmello
        sorry.. I copied wrong.. it's related_name='tech'
      • and not selected_related
      • lemme try the exclude
      • FunkyBob
        Person.objects.filter(instructors__isnull=False) try that, then try exclude
      • that related name doesn't make a lot of sense, semantically
      • mmello
        I'm translating the names because the code is written in Portguese
      • bchhun joined the channel
      • bchhun has left the channel
      • with exclude()
      • FunkyBob
        ok
      • mmello
        same 91
      • lemme show you in pt_BR the objects
      • FunkyBob
        you need to use the related name
      • bender314 has quit
      • RileyII
        I'm having doubts that your data looks the way you think it looks.
      • gazumps has quit
      • mmello
        len(Filiado.objects.filter(entidade__isnull=False).exclude(entidade__isnull=True))
      • 91
      • where Filiado = Person
      • Entidade = Org
      • lemme pastebin
      • FunkyBob
        you only need one of filter or exclude
      • mmello
        len(Filiado.objects.exclude(entidade__isnull=True))
      • FunkyBob
        don't forget to pastebin the actual models, please
      • mmello
        91
      • oubiga has quit
      • yes.. doing now
      • backjlack has quit
      • schinckel
        I wish south had the ability to handle a --edit flag, that would open the newly created migration in your $EDITOR
      • mmello
      • FunkyBob
        schinckel: ooh, that'd be nice
      • mmello
        here is in pt_BR
      • schinckel
        Guess it is something I should bug andrew about for the new stuff, though.
      • Leeds joined the channel
      • Gentlecat joined the channel
      • RileyII
        mmello: Have you looked at your data to confirm that it looks the way you expect?
      • joshfinnie has quit
      • mmello
        RileyII: yes.. I checked
      • lemme do a for to show you
      • look
      • joshfinnie joined the channel
      • QuanSai
        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?
      • mmello
        RileyII: makes sense now