Django ORM join many to many relation in one query












3















If we have 2 models A, B with a many to many relation.



I want to obtain a sql query similar to this:



SELECT *
FROM a LEFT JOIN ab_relation
ON ab_relation.a_id = a.id
JOIN b ON ab_relation.b_id = b.id;


So in django when I try:



A.objects.prefetch_related('bees')


I get 2 queries similar to:



SELECT * FROM a;
SELECT ab_relation.a_id AS prefetch_related_val_a_id, b.*
FROM b JOIN ab_relation ON b.id = ab_relation.b_id
WHERE ab_relation.a_id IN (123, 456... list of all a.id);


Given that A and B have moderately big tables, I find the way django does it too slow for my needs.



The question is: Is it possible to obtain the left join manually written query through the ORM?



Edits to answer some clarifications:




  • Yes a LEFT OUTER JOIN would be preferable to get all A's in the queryset, not only those with a relation with B (updated sql).


  • Moderately big means ~4k rows each, and too slow means ~3 seconds (on first load, before redis cache.) Keep in mind there are other queries on the page.


  • Actually yes, we need only B.one_field but having tried with Prefetch('bees', queryset=B.objects.values('one_field')) an error said you can't use values in a prefetch.


  • The queryset will be used as options for a multi-select form-field, where we will need to represent A objects that have a relation with B with an extra string from the B.field.











share|improve this question





























    3















    If we have 2 models A, B with a many to many relation.



    I want to obtain a sql query similar to this:



    SELECT *
    FROM a LEFT JOIN ab_relation
    ON ab_relation.a_id = a.id
    JOIN b ON ab_relation.b_id = b.id;


    So in django when I try:



    A.objects.prefetch_related('bees')


    I get 2 queries similar to:



    SELECT * FROM a;
    SELECT ab_relation.a_id AS prefetch_related_val_a_id, b.*
    FROM b JOIN ab_relation ON b.id = ab_relation.b_id
    WHERE ab_relation.a_id IN (123, 456... list of all a.id);


    Given that A and B have moderately big tables, I find the way django does it too slow for my needs.



    The question is: Is it possible to obtain the left join manually written query through the ORM?



    Edits to answer some clarifications:




    • Yes a LEFT OUTER JOIN would be preferable to get all A's in the queryset, not only those with a relation with B (updated sql).


    • Moderately big means ~4k rows each, and too slow means ~3 seconds (on first load, before redis cache.) Keep in mind there are other queries on the page.


    • Actually yes, we need only B.one_field but having tried with Prefetch('bees', queryset=B.objects.values('one_field')) an error said you can't use values in a prefetch.


    • The queryset will be used as options for a multi-select form-field, where we will need to represent A objects that have a relation with B with an extra string from the B.field.











    share|improve this question



























      3












      3








      3


      1






      If we have 2 models A, B with a many to many relation.



      I want to obtain a sql query similar to this:



      SELECT *
      FROM a LEFT JOIN ab_relation
      ON ab_relation.a_id = a.id
      JOIN b ON ab_relation.b_id = b.id;


      So in django when I try:



      A.objects.prefetch_related('bees')


      I get 2 queries similar to:



      SELECT * FROM a;
      SELECT ab_relation.a_id AS prefetch_related_val_a_id, b.*
      FROM b JOIN ab_relation ON b.id = ab_relation.b_id
      WHERE ab_relation.a_id IN (123, 456... list of all a.id);


      Given that A and B have moderately big tables, I find the way django does it too slow for my needs.



      The question is: Is it possible to obtain the left join manually written query through the ORM?



      Edits to answer some clarifications:




      • Yes a LEFT OUTER JOIN would be preferable to get all A's in the queryset, not only those with a relation with B (updated sql).


      • Moderately big means ~4k rows each, and too slow means ~3 seconds (on first load, before redis cache.) Keep in mind there are other queries on the page.


      • Actually yes, we need only B.one_field but having tried with Prefetch('bees', queryset=B.objects.values('one_field')) an error said you can't use values in a prefetch.


      • The queryset will be used as options for a multi-select form-field, where we will need to represent A objects that have a relation with B with an extra string from the B.field.











      share|improve this question
















      If we have 2 models A, B with a many to many relation.



      I want to obtain a sql query similar to this:



      SELECT *
      FROM a LEFT JOIN ab_relation
      ON ab_relation.a_id = a.id
      JOIN b ON ab_relation.b_id = b.id;


      So in django when I try:



      A.objects.prefetch_related('bees')


      I get 2 queries similar to:



      SELECT * FROM a;
      SELECT ab_relation.a_id AS prefetch_related_val_a_id, b.*
      FROM b JOIN ab_relation ON b.id = ab_relation.b_id
      WHERE ab_relation.a_id IN (123, 456... list of all a.id);


      Given that A and B have moderately big tables, I find the way django does it too slow for my needs.



      The question is: Is it possible to obtain the left join manually written query through the ORM?



      Edits to answer some clarifications:




      • Yes a LEFT OUTER JOIN would be preferable to get all A's in the queryset, not only those with a relation with B (updated sql).


      • Moderately big means ~4k rows each, and too slow means ~3 seconds (on first load, before redis cache.) Keep in mind there are other queries on the page.


      • Actually yes, we need only B.one_field but having tried with Prefetch('bees', queryset=B.objects.values('one_field')) an error said you can't use values in a prefetch.


      • The queryset will be used as options for a multi-select form-field, where we will need to represent A objects that have a relation with B with an extra string from the B.field.








      django postgresql django-models django-orm






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 9:42







      David Veza

















      asked Nov 20 '18 at 8:15









      David VezaDavid Veza

      1249




      1249
























          1 Answer
          1






          active

          oldest

          votes


















          2














          For the direct answer skip to point 6)



          Let'ts talk step by step.



          1) N:M select. You say you want a query like this:



          SELECT *
          FROM a JOIN ab_relation ON ab_relation.a_id = a.id
          JOIN b ON ab_relation.b_id = b.id;


          But this is not a real N:M query, because you are getting only A-B related objects The query should use outer joins. At least like:



          SELECT *
          FROM a left outer JOIN
          ab_relation ON ab_relation.a_id = a.id left outer JOIN
          b ON ab_relation.b_id = b.id;


          In other cases you are getting only A models with a related B.



          2) Read big tables You say "moderately big tables". Then, are you sure you want to read the whole table from database? This is not usual on a web environment to read a lot of data, and, in this case, you can paginate data. May be is not a web app? Why you need to read this big tables? We need context to answer your question. Are you sure you need all fields from both tables?



          3) Select * from Are you sure you need all fields from both tables? May be if you read only some values this query will run faster.



          A.objects.values( "some_a_field", "anoter_a_field", "Bs__some_b_field" )


          4) As summary. ORM is a powerful tool, two single read operations are "fast". I write some ideas but perhaps we need more context to answer your question. What means moderate big tables, wheat means slow, what are you doing with this data, how many fields or bytes has each row from each table, ... .



          Editedd Because OP has edited the question.



          5) Use right UI controls. You say:




          The queryset will be used as options for a multi-select form-field, where we will need to represent A objects that have a relation with B with an extra string from the B.field.




          It looks like an anti-pattern to send to client 4k rows for a form. I suggest to you to move to a live control that loads only needed data. For example, filtering by some text. Take a look to django-select2 awesome project.



          6) You say




          The question is: Is it possible to obtain the left join manually written query through the ORM?




          The answer is: Yes, you can do it using values, as I said it on point 3. Sample: Material and ResultatAprenentatge is a N:M relation:



          >>> print( Material
          .objects
          .values( "titol", "resultats_aprenentatge__codi" )
          .query )


          The query:



          SELECT "material_material"."titol", 
          "ufs_resultataprenentatge"."codi"
          FROM "material_material"
          LEFT OUTER JOIN "material_material_resultats_aprenentatge"
          ON ( "material_material"."id" =
          "material_material_resultats_aprenentatge"."material_id" )
          LEFT OUTER JOIN "ufs_resultataprenentatge"
          ON (
          "material_material_resultats_aprenentatge"."resultataprenentatge_id" =
          "ufs_resultataprenentatge"."id" )
          ORDER BY "material_material"."data_edicio" DESC





          share|improve this answer


























          • Thanks, updated the question with some clarifications for your questions.

            – David Veza
            Nov 20 '18 at 9:07











          • @DavidVeza, I updated my answer consequently.

            – dani herrera
            Nov 20 '18 at 9:14











          • I am aware that filtering through xhr can be a solution, but that still does not answer the question.

            – David Veza
            Nov 20 '18 at 9:40











          • @DavidVeza, yes, I did, on point 3) also now on point 6)

            – dani herrera
            Nov 20 '18 at 10:02











          • @DavidVeza, I have changed the sample for an N:M sample.

            – dani herrera
            Nov 20 '18 at 10:22













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388750%2fdjango-orm-join-many-to-many-relation-in-one-query%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          For the direct answer skip to point 6)



          Let'ts talk step by step.



          1) N:M select. You say you want a query like this:



          SELECT *
          FROM a JOIN ab_relation ON ab_relation.a_id = a.id
          JOIN b ON ab_relation.b_id = b.id;


          But this is not a real N:M query, because you are getting only A-B related objects The query should use outer joins. At least like:



          SELECT *
          FROM a left outer JOIN
          ab_relation ON ab_relation.a_id = a.id left outer JOIN
          b ON ab_relation.b_id = b.id;


          In other cases you are getting only A models with a related B.



          2) Read big tables You say "moderately big tables". Then, are you sure you want to read the whole table from database? This is not usual on a web environment to read a lot of data, and, in this case, you can paginate data. May be is not a web app? Why you need to read this big tables? We need context to answer your question. Are you sure you need all fields from both tables?



          3) Select * from Are you sure you need all fields from both tables? May be if you read only some values this query will run faster.



          A.objects.values( "some_a_field", "anoter_a_field", "Bs__some_b_field" )


          4) As summary. ORM is a powerful tool, two single read operations are "fast". I write some ideas but perhaps we need more context to answer your question. What means moderate big tables, wheat means slow, what are you doing with this data, how many fields or bytes has each row from each table, ... .



          Editedd Because OP has edited the question.



          5) Use right UI controls. You say:




          The queryset will be used as options for a multi-select form-field, where we will need to represent A objects that have a relation with B with an extra string from the B.field.




          It looks like an anti-pattern to send to client 4k rows for a form. I suggest to you to move to a live control that loads only needed data. For example, filtering by some text. Take a look to django-select2 awesome project.



          6) You say




          The question is: Is it possible to obtain the left join manually written query through the ORM?




          The answer is: Yes, you can do it using values, as I said it on point 3. Sample: Material and ResultatAprenentatge is a N:M relation:



          >>> print( Material
          .objects
          .values( "titol", "resultats_aprenentatge__codi" )
          .query )


          The query:



          SELECT "material_material"."titol", 
          "ufs_resultataprenentatge"."codi"
          FROM "material_material"
          LEFT OUTER JOIN "material_material_resultats_aprenentatge"
          ON ( "material_material"."id" =
          "material_material_resultats_aprenentatge"."material_id" )
          LEFT OUTER JOIN "ufs_resultataprenentatge"
          ON (
          "material_material_resultats_aprenentatge"."resultataprenentatge_id" =
          "ufs_resultataprenentatge"."id" )
          ORDER BY "material_material"."data_edicio" DESC





          share|improve this answer


























          • Thanks, updated the question with some clarifications for your questions.

            – David Veza
            Nov 20 '18 at 9:07











          • @DavidVeza, I updated my answer consequently.

            – dani herrera
            Nov 20 '18 at 9:14











          • I am aware that filtering through xhr can be a solution, but that still does not answer the question.

            – David Veza
            Nov 20 '18 at 9:40











          • @DavidVeza, yes, I did, on point 3) also now on point 6)

            – dani herrera
            Nov 20 '18 at 10:02











          • @DavidVeza, I have changed the sample for an N:M sample.

            – dani herrera
            Nov 20 '18 at 10:22


















          2














          For the direct answer skip to point 6)



          Let'ts talk step by step.



          1) N:M select. You say you want a query like this:



          SELECT *
          FROM a JOIN ab_relation ON ab_relation.a_id = a.id
          JOIN b ON ab_relation.b_id = b.id;


          But this is not a real N:M query, because you are getting only A-B related objects The query should use outer joins. At least like:



          SELECT *
          FROM a left outer JOIN
          ab_relation ON ab_relation.a_id = a.id left outer JOIN
          b ON ab_relation.b_id = b.id;


          In other cases you are getting only A models with a related B.



          2) Read big tables You say "moderately big tables". Then, are you sure you want to read the whole table from database? This is not usual on a web environment to read a lot of data, and, in this case, you can paginate data. May be is not a web app? Why you need to read this big tables? We need context to answer your question. Are you sure you need all fields from both tables?



          3) Select * from Are you sure you need all fields from both tables? May be if you read only some values this query will run faster.



          A.objects.values( "some_a_field", "anoter_a_field", "Bs__some_b_field" )


          4) As summary. ORM is a powerful tool, two single read operations are "fast". I write some ideas but perhaps we need more context to answer your question. What means moderate big tables, wheat means slow, what are you doing with this data, how many fields or bytes has each row from each table, ... .



          Editedd Because OP has edited the question.



          5) Use right UI controls. You say:




          The queryset will be used as options for a multi-select form-field, where we will need to represent A objects that have a relation with B with an extra string from the B.field.




          It looks like an anti-pattern to send to client 4k rows for a form. I suggest to you to move to a live control that loads only needed data. For example, filtering by some text. Take a look to django-select2 awesome project.



          6) You say




          The question is: Is it possible to obtain the left join manually written query through the ORM?




          The answer is: Yes, you can do it using values, as I said it on point 3. Sample: Material and ResultatAprenentatge is a N:M relation:



          >>> print( Material
          .objects
          .values( "titol", "resultats_aprenentatge__codi" )
          .query )


          The query:



          SELECT "material_material"."titol", 
          "ufs_resultataprenentatge"."codi"
          FROM "material_material"
          LEFT OUTER JOIN "material_material_resultats_aprenentatge"
          ON ( "material_material"."id" =
          "material_material_resultats_aprenentatge"."material_id" )
          LEFT OUTER JOIN "ufs_resultataprenentatge"
          ON (
          "material_material_resultats_aprenentatge"."resultataprenentatge_id" =
          "ufs_resultataprenentatge"."id" )
          ORDER BY "material_material"."data_edicio" DESC





          share|improve this answer


























          • Thanks, updated the question with some clarifications for your questions.

            – David Veza
            Nov 20 '18 at 9:07











          • @DavidVeza, I updated my answer consequently.

            – dani herrera
            Nov 20 '18 at 9:14











          • I am aware that filtering through xhr can be a solution, but that still does not answer the question.

            – David Veza
            Nov 20 '18 at 9:40











          • @DavidVeza, yes, I did, on point 3) also now on point 6)

            – dani herrera
            Nov 20 '18 at 10:02











          • @DavidVeza, I have changed the sample for an N:M sample.

            – dani herrera
            Nov 20 '18 at 10:22
















          2












          2








          2







          For the direct answer skip to point 6)



          Let'ts talk step by step.



          1) N:M select. You say you want a query like this:



          SELECT *
          FROM a JOIN ab_relation ON ab_relation.a_id = a.id
          JOIN b ON ab_relation.b_id = b.id;


          But this is not a real N:M query, because you are getting only A-B related objects The query should use outer joins. At least like:



          SELECT *
          FROM a left outer JOIN
          ab_relation ON ab_relation.a_id = a.id left outer JOIN
          b ON ab_relation.b_id = b.id;


          In other cases you are getting only A models with a related B.



          2) Read big tables You say "moderately big tables". Then, are you sure you want to read the whole table from database? This is not usual on a web environment to read a lot of data, and, in this case, you can paginate data. May be is not a web app? Why you need to read this big tables? We need context to answer your question. Are you sure you need all fields from both tables?



          3) Select * from Are you sure you need all fields from both tables? May be if you read only some values this query will run faster.



          A.objects.values( "some_a_field", "anoter_a_field", "Bs__some_b_field" )


          4) As summary. ORM is a powerful tool, two single read operations are "fast". I write some ideas but perhaps we need more context to answer your question. What means moderate big tables, wheat means slow, what are you doing with this data, how many fields or bytes has each row from each table, ... .



          Editedd Because OP has edited the question.



          5) Use right UI controls. You say:




          The queryset will be used as options for a multi-select form-field, where we will need to represent A objects that have a relation with B with an extra string from the B.field.




          It looks like an anti-pattern to send to client 4k rows for a form. I suggest to you to move to a live control that loads only needed data. For example, filtering by some text. Take a look to django-select2 awesome project.



          6) You say




          The question is: Is it possible to obtain the left join manually written query through the ORM?




          The answer is: Yes, you can do it using values, as I said it on point 3. Sample: Material and ResultatAprenentatge is a N:M relation:



          >>> print( Material
          .objects
          .values( "titol", "resultats_aprenentatge__codi" )
          .query )


          The query:



          SELECT "material_material"."titol", 
          "ufs_resultataprenentatge"."codi"
          FROM "material_material"
          LEFT OUTER JOIN "material_material_resultats_aprenentatge"
          ON ( "material_material"."id" =
          "material_material_resultats_aprenentatge"."material_id" )
          LEFT OUTER JOIN "ufs_resultataprenentatge"
          ON (
          "material_material_resultats_aprenentatge"."resultataprenentatge_id" =
          "ufs_resultataprenentatge"."id" )
          ORDER BY "material_material"."data_edicio" DESC





          share|improve this answer















          For the direct answer skip to point 6)



          Let'ts talk step by step.



          1) N:M select. You say you want a query like this:



          SELECT *
          FROM a JOIN ab_relation ON ab_relation.a_id = a.id
          JOIN b ON ab_relation.b_id = b.id;


          But this is not a real N:M query, because you are getting only A-B related objects The query should use outer joins. At least like:



          SELECT *
          FROM a left outer JOIN
          ab_relation ON ab_relation.a_id = a.id left outer JOIN
          b ON ab_relation.b_id = b.id;


          In other cases you are getting only A models with a related B.



          2) Read big tables You say "moderately big tables". Then, are you sure you want to read the whole table from database? This is not usual on a web environment to read a lot of data, and, in this case, you can paginate data. May be is not a web app? Why you need to read this big tables? We need context to answer your question. Are you sure you need all fields from both tables?



          3) Select * from Are you sure you need all fields from both tables? May be if you read only some values this query will run faster.



          A.objects.values( "some_a_field", "anoter_a_field", "Bs__some_b_field" )


          4) As summary. ORM is a powerful tool, two single read operations are "fast". I write some ideas but perhaps we need more context to answer your question. What means moderate big tables, wheat means slow, what are you doing with this data, how many fields or bytes has each row from each table, ... .



          Editedd Because OP has edited the question.



          5) Use right UI controls. You say:




          The queryset will be used as options for a multi-select form-field, where we will need to represent A objects that have a relation with B with an extra string from the B.field.




          It looks like an anti-pattern to send to client 4k rows for a form. I suggest to you to move to a live control that loads only needed data. For example, filtering by some text. Take a look to django-select2 awesome project.



          6) You say




          The question is: Is it possible to obtain the left join manually written query through the ORM?




          The answer is: Yes, you can do it using values, as I said it on point 3. Sample: Material and ResultatAprenentatge is a N:M relation:



          >>> print( Material
          .objects
          .values( "titol", "resultats_aprenentatge__codi" )
          .query )


          The query:



          SELECT "material_material"."titol", 
          "ufs_resultataprenentatge"."codi"
          FROM "material_material"
          LEFT OUTER JOIN "material_material_resultats_aprenentatge"
          ON ( "material_material"."id" =
          "material_material_resultats_aprenentatge"."material_id" )
          LEFT OUTER JOIN "ufs_resultataprenentatge"
          ON (
          "material_material_resultats_aprenentatge"."resultataprenentatge_id" =
          "ufs_resultataprenentatge"."id" )
          ORDER BY "material_material"."data_edicio" DESC






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 16:29









          David Veza

          1249




          1249










          answered Nov 20 '18 at 8:31









          dani herreradani herrera

          28.1k348108




          28.1k348108













          • Thanks, updated the question with some clarifications for your questions.

            – David Veza
            Nov 20 '18 at 9:07











          • @DavidVeza, I updated my answer consequently.

            – dani herrera
            Nov 20 '18 at 9:14











          • I am aware that filtering through xhr can be a solution, but that still does not answer the question.

            – David Veza
            Nov 20 '18 at 9:40











          • @DavidVeza, yes, I did, on point 3) also now on point 6)

            – dani herrera
            Nov 20 '18 at 10:02











          • @DavidVeza, I have changed the sample for an N:M sample.

            – dani herrera
            Nov 20 '18 at 10:22





















          • Thanks, updated the question with some clarifications for your questions.

            – David Veza
            Nov 20 '18 at 9:07











          • @DavidVeza, I updated my answer consequently.

            – dani herrera
            Nov 20 '18 at 9:14











          • I am aware that filtering through xhr can be a solution, but that still does not answer the question.

            – David Veza
            Nov 20 '18 at 9:40











          • @DavidVeza, yes, I did, on point 3) also now on point 6)

            – dani herrera
            Nov 20 '18 at 10:02











          • @DavidVeza, I have changed the sample for an N:M sample.

            – dani herrera
            Nov 20 '18 at 10:22



















          Thanks, updated the question with some clarifications for your questions.

          – David Veza
          Nov 20 '18 at 9:07





          Thanks, updated the question with some clarifications for your questions.

          – David Veza
          Nov 20 '18 at 9:07













          @DavidVeza, I updated my answer consequently.

          – dani herrera
          Nov 20 '18 at 9:14





          @DavidVeza, I updated my answer consequently.

          – dani herrera
          Nov 20 '18 at 9:14













          I am aware that filtering through xhr can be a solution, but that still does not answer the question.

          – David Veza
          Nov 20 '18 at 9:40





          I am aware that filtering through xhr can be a solution, but that still does not answer the question.

          – David Veza
          Nov 20 '18 at 9:40













          @DavidVeza, yes, I did, on point 3) also now on point 6)

          – dani herrera
          Nov 20 '18 at 10:02





          @DavidVeza, yes, I did, on point 3) also now on point 6)

          – dani herrera
          Nov 20 '18 at 10:02













          @DavidVeza, I have changed the sample for an N:M sample.

          – dani herrera
          Nov 20 '18 at 10:22







          @DavidVeza, I have changed the sample for an N:M sample.

          – dani herrera
          Nov 20 '18 at 10:22






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388750%2fdjango-orm-join-many-to-many-relation-in-one-query%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud