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 5928243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:16:12+00:00 2026-05-22T14:16:12+00:00

I want to execute this query of three models/tables: — get all items that

  • 0

I want to execute this query of three models/tables:

-- get all items that don't have a status for this member
SELECT  i.*
FROM
 mock_item i
 LEFT JOIN mock_itemstatusmember ism ON i.id = ism.item_id AND ism.member_id = 2

WHERE
 ism.id IS NULL

Here are how my models look:

from django.db import models

# think status as a status
class Member(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)

class Status(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    no = models.SmallIntegerField()

class Item(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    status = models.ManyToManyField(Status, through='ItemStatusMember')
    member = models.ManyToManyField(Member, through='ItemStatusMember')    


class ItemStatusMember(models.Model):
    """
    Table that keeps job statuss information for a user.
    Tides Items with Members with Statuss
    """
    id = models.AutoField(primary_key=True)
    member = models.ForeignKey(Member)
    status = models.ForeignKey(Status)
    item = models.ForeignKey(Item)

And their database sample data:

mysql> select *  from mock_member;
+----+--------+
| id | name   |
+----+--------+
|  1 | Stefan |
|  2 | Alex   |
|  3 | Diana  |
+----+--------+
3 rows in set (0.00 sec)

mysql> select *  from mock_status;
+----+---------+----+
| id | name    | no |
+----+---------+----+
|  1 | Pending |  1 |
|  2 | Success |  2 |
+----+---------+----+
2 rows in set (0.00 sec)

mysql> select *  from mock_item;
+----+----------------+
| id | name           |
+----+----------------+
|  1 | My first item  |
|  2 | My second item |
|  3 | My third item  |
+----+----------------+
3 rows in set (0.00 sec)

mysql> select *  from mock_itemstatusmember;
+----+-----------+-----------+---------+
| id | status_id | member_id | item_id |
+----+-----------+-----------+---------+
|  1 |         1 |         1 |       1 |
|  2 |         2 |         1 |       2 |
|  3 |         1 |         2 |       1 |
+----+-----------+-----------+---------+
3 rows in set (0.00 sec)

I need this, with the ORM, and NOT with the raw SQL query facility from the ORM to get all items that don’t have a status for this member.

The SQL query at the beginning of the question does this, so it’s important to have the

AND ism.member_id = 2

condition as a second condition to the left-join part, instead of moving into where, which I know how to do.

  • 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. Editorial Team
    Editorial Team
    2026-05-22T14:16:12+00:00Added an answer on May 22, 2026 at 2:16 pm
    from django.db.models import Q    
    Item.objects.filter(~Q(member__id=2))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute a query like this var result = from entry in
I want to execute this script (view source) that uses Google Translate AJAX API
I have the following MySQL query, which produces the result I want: SELECT `l`.`status`,
I have an Oracle table with data that looks like this: ID BATCH STATUS
When I want to execute Rails 3 console, it throws this: $ script/rails c
I have asp.net generated html code. I want execute jQuery click function when user
I want to execute a function on all the objects within a List of
I want to execute some Python code, typed at runtime, so I get the
I have some calls that must execute sequentially. Consider an IService that has a
Suppose that I have a database query which looks like below - select name,

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.