Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 64127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:44:28+00:00 2026-05-10T18:44:28+00:00

How can I create a query for a full outer join across a M2M

  • 0

How can I create a query for a full outer join across a M2M relationchip using the django QuerySet API?

It that is not supported, some hint about creating my own manager to do this would be welcome.

Edited to add: @S.Lott: Thanks for the enlightenment. The need for the OUTER JOIN comes from the application. It has to generate a report showing the data entered, even if it still incomplete. I was not aware of the fact that the result would be a new class/model. Your hints will help me quite a bit.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:44:29+00:00Added an answer on May 10, 2026 at 6:44 pm

    Django doesn’t support ‘joins’ in the usual SQL sense — it supports object navigation.

    Note that a relational join (inner or outer) creates a new ‘class’ of entities. One that doesn’t have a definition in Django. So there’s no proper ‘result set’ since there’s no class definition for the things you get back. The best you can do is define a tuple which will be packed with None’s for missing combinations.

    A left (or right) outer join looks like this. It creates two disjoint subsets, those who have an associated set of related entities, and those who don’t.

    for obj in Model1.objects.all():     if obj.model2_set().count() == 0:         # process (obj, None) -- no Model2 association     else:         for obj2 in obj.model2_set.all():             # process (obj, obj2) -- the 'inner join' result 

    A ‘Full’ outer join is a union of the remaining items that have no relationships.

    for obj2 in Model2.objects.all():     if obj2.model1_set().count() == 0:         # process (None, obj2) -- no Model1 association 

    The issue is always, what processing are you doing with this weird collection of three different subsets of objects?

    The point of an object database is to focus the processing on the object and it’s associated objects.

    The peculiar collection called a ‘relational join’ is never in the original object model. It’s a new class of objects built from two (or more) original objects.

    Worse, outer joins create a collection with multiple subclasses (inner join, left outer join and right outer join). What does that collection of things mean?

    Wait, it can get worse. If the processing includes checks for the missing attributes (i.e. if someObj.anObj2attribute is None: we’re essentially looking for Model1 items with no Model2 object associated. Ummm… why did we put those in the outer join, only to filter them using an if statement? Why not just do separate queries amd process each subset properly?


    Edit: When you’re showing ‘incomplete’ status, it isn’t an outer-join at all. It’s much simpler. You need to create one (or two) separate collections in your view function for your template to display.

    First, you should use status codes, not the presence or absence of a foreign key. Optional foreign keys don’t have ‘reasons’ — they’re either there or not there. A status code can provide useful shades of meaning (‘incomplete’, ‘in error’, ‘broken’, ‘not applicable’, ‘to be deleted’, etc.)

    errorList1 = Model1.objects.filter( status='Incomplete' ) errorList2 = Model2.objects.filter( status='Incomplete' ) 

    These two are the two non-join parts of a full outer join. You can then display these two error lists in your template with appropriate column titles and status codes and everything.

    You can even put them into a single table to mimic the old full outer join report people used to see

    <table>     <tr><th>Model1</th><th>Model2</th></tr>     {% for e1 in errorList1 %}     <tr><td>e1</td><td>NULL</td></tr>     {% endfor %}     {% for e2 in errorList2 %}     <tr><td>NULL</td><td>e2</td></tr>     {% endfor %} </table> 

    Looks like a full outer join report. Without the full outer join.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 137k
  • Answers 137k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Don't Dock your User control. Use the Anchor property instead.… May 12, 2026 at 7:26 am
  • Editorial Team
    Editorial Team added an answer Portable? I don't think so. Maybe the closest you can… May 12, 2026 at 7:26 am
  • Editorial Team
    Editorial Team added an answer Something like this? import csv source= csv.reader( open("some file","rb") )… May 12, 2026 at 7:26 am

Related Questions

I have title (varchar), description (text), keywords (varchar) fields in my mysql table. I
I've got a bit of a challenge where I have to create an expression
The project currently I am working in requires a lot of searhing/filtering pages. For
I am using ruby on rails with a MySQL backend. I have a table

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.