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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:32:50+00:00 2026-05-31T19:32:50+00:00

I have some models that are laid out like so (with a lot more

  • 0

I have some models that are laid out like so (with a lot more fields):

class A(models.Model):
    name = models.
class B(models.Model):
    foo = models.ForeignKey(A)

and I have a search system that uses Q objects; 0 and 1 are primary keys, obtained by a ModelMultipleChoiceField in a form, and both of their foos point to the same A.

So for or logic, I can use this

>>> A.objects.filter(Q(b__pk=0) | Q(b__pk=1))
[<A: A object>, <A: A object>]

and I can fix the duplication by using

A.objects.filter(Q(b__pk=0) | Q(b__pk=1)).distinct()

However, I also want to do and logic. Ideally it would work by using & instead of |, but that doesn’t work.

>>> A.objects.filter(Q(b__pk=0) & Q(b__pk=1))
[]

So, what is the simplest way to do this?

  • 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-31T19:32:52+00:00Added an answer on May 31, 2026 at 7:32 pm

    Guess you want A instances that having at least one B() w/ primary key 1 and one B() w/ primary key 0

    Then you need

    (A.objects.filter(b__pk=0) & A.objects.filter(b__pk=1)).distinct()
    

    Or the following if join hurts

    A.objects.filter(
        models.Q(pk__in=B.objects.filter(pk=0).values('foo_id')) &
        models.Q(pk__in=B.objects.filter(pk=1).values('foo_id')))
    

    If you need A() that having only two relative B() one w/ pk=0 and another pk=1, limitation should be applied to the above queryset by

    queryset.annotate(b_count=models.Count('b')).filter(b_count=2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some models like that: class BaseModel(models.Model): created_by = models.ForeignKey(User, related_name=%(app_label)s_%(class)s_created) created_date =
I have some models that look like this: class UserProfile(models.Model): user = models.OneToOneField(User) favorite_books
I have some models and have views code that look like the following class
I have some Django models with a structure similar to this: class Grandparent(models.Model): name
If you have some models: class Teacher(models.Model): name = models.CharField(max_length=50) class Student(models.Model): age =
I have some nested models that look something like: class Company has_many :managers end
Say I have some django models, something like this: class Address(models.Model): pass class Person(models.Model):
I have a models A and B , that are like this: class A(models.Model):
Let's say I have some contrived models: class Author(Model): name = CharField() class Book(Model):
Let's say I have some model objects that resemble this: public class FooModel {

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.