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

  • SEARCH
  • Home
  • 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 3631870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:27:11+00:00 2026-05-19T00:27:11+00:00

These are the (pseudo) models I’ve got. Blog: name etc… Article: name blog creator

  • 0

These are the (pseudo) models I’ve got.

Blog:
  name
  etc...

Article:
  name
  blog
  creator
  etc

User (as per django.contrib.auth)

So my problem is this: I’ve got two users. I want to get all of the articles that the two users published on the same blog (no matter which blog). I can’t simply filter the Article model by both users, because that would yield the set of Articles created by both users. Obviously not what I want. but can I filter somehow to get all of the articles where a field of the object matches between the two querysets?

  • 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-19T00:27:11+00:00Added an answer on May 19, 2026 at 12:27 am

    This is a great question for exercising Django’s ORM 🙂

    Depending on which parameters are known in advance, there are a couple ways to approach this.

    Scenario 1: You know the users and the specific blog

    If you have one particular blog in mind and want to simple find all articles written by either author, you can use a Q object. I don’t think this is your scenario, but I’ll put it here just in case:

    from django.db.models import Q
    Article.objects.filter(Q(creator=user1) | Q(creator=user2), blog=some_blog_instance)
    

    Scenario 2: You know only the users

    If you want to find all blogs where both users have published, and want to then find which articles they’ve published in those blogs, you’d want to start from the Blog model:

    from django.db.models import Q
    
    # Find all blogs where both user1 and user2 have written articles
    blogs = Blog.objects.filter(article__creator=user1).\
            filter(article__creator=user2).distinct()
    
    # Now find which articles those were
    for blog in blogs:
        articles = blog.article_set.filter(Q=(creator=user1) | Q=(creator=user2))
    

    Edit based on Paulo’s comments:

    Here is a test set of models that I believe matches the OP’s pseudo code and which demonstrates the above code working (at least on sqlite3 and postgres):

    from django.db import models
    from django.contrib.auth.models import User
    
    class Blog(models.Model):
        name = models.CharField(max_length=128)
    
    class Article(models.Model):
        name = models.CharField(max_length=128)
        blog = models.ForeignKey(Blog)
        creator = models.ForeignKey(User)
    

    Then some data where both user1 and user2 write articles on blog2:

    u1 = User.objects.create(username='u1')
    u2 = User.objects.create(username='u2')
    
    b1 = Blog.objects.create(name='b1')
    b2 = Blog.objects.create(name='b2')
    b3 = Blog.objects.create(name='b3')
    
    b1_art1 = Article.objects.create(name='b1_art1', blog=b1, creator=u1)
    b2_art1 = Article.objects.create(name='b2_art1', blog=b2, creator=u1)
    b2_art2 = Article.objects.create(name='b2_art2', blog=b2, creator=u2)
    

    The query:

    [b.name for b in Blog.objects.filter(article__creator=user1).\
        filter(article__creator=user2).distinct()]
    

    produces:

    [b2]
    

    And the SQL is (my test django app was named foo):

    SELECT "foo_blog"."id", "foo_blog"."name" 
    FROM "foo_blog" 
    INNER JOIN "foo_article" ON ("foo_blog"."id" = "foo_article"."blog_id") 
    INNER JOIN "foo_article" T4 ON ("foo_blog"."id" = T4."blog_id") 
    WHERE ("foo_article"."creator_id" = 1  AND T4."creator_id" = 2 )
    

    So, while the WHERE clause does have (A AND B), A and B reference different inner joins ("foo_article"."creator_id" vs. T4."creator_id"), and not the same table (which is what filter(A, B) would generate, (ie: WHERE ("foo_article"."creator_id" = 1 and "foo_article"."creator_id" = 2))

    (Without the distinct() clause, if you added more articles for either author, you’d get multiple b2 entries in the queryset results.)

    Like I said, it’s a great ORM exercise!

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

Sidebar

Related Questions

Consider these pseudo models: class BaseProduct: quantity_available = Integer class Box(BaseProduct): items_in_box = Integer
I'm using Django/Python, but pseudo-code is definitely acceptable here. Working with some models that
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =
This is a bit of a puzzle, I have these pseudo models: class Country(models.Model):
In my Rails application I have these models Order has_many OrderItem OrderItem belongs_to Order
I realized I'm using these pseudo-classes pretty often, hence my question: In 2012, should
So I have the following models: User Business Coupons Deals Redeemable owner I am
Is there a way for me to get a pseudo-ID of a post from
Provide an example for the pseudo-regex: Match every url except those from example.com and
On linux, I am opening a pseudo tty on the master side. While there

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.