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

The Archive Base Latest Questions

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

I have 2 django models: class Foo(models.Model): baz = models.CharField() class Bar(models.Model); foo =

  • 0

I have 2 django models:

class Foo(models.Model):
    baz = models.CharField()

class Bar(models.Model);
    foo = models.ForeignKey(Foo)
    qux = models.CharField()

with the following data:

Foo
id baz
4  X
5  Y
6  Z

Bar
id foo_id qux
1  4      A
2  5      A
3  5      B
4  6      B

now I do 2 queries on Bar, filtered on qux:

resA = [1, 2] (actually bar instances; shown bar.id for convenience)
resB = [3, 4] (actually bar instances; shown bar.id for convenience)

What is now the fastest way to AND these lists together so that the result will be:

resAND = [5] (foo.id)

Right now I do:

ret = []
nr_sets = 2
foos = Foo.objects.all()
bars = list(resA + resB)
for foo in foos:
    test = filter(lambda bar : bar.foo_id == foo.id, bars)
    if test == nr_sets;
        ret.append(foo)

This is however horribly slow. Any ideas in speeding this up? I am particularly looking for post query solutions, but good ideas concerning the query(‘s) are also welcome.

  • 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-28T01:19:45+00:00Added an answer on May 28, 2026 at 1:19 am

    You could try

    foo_ids = set(resA.values_list('foo_id', flat=True)) & \
        set(resB.values_list('foo_id', flat=True))
    

    Then you can get the corresponding complete Foo objects with

    ret = Foo.objects.in_bulk(foo_ids).values()
    

    Edit: used set because apparently & does not work as expected on ValueQuerySet.

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

Sidebar

Related Questions

Say I have the following models: class Baz(models.Model): winning = models.CharField(max_length=100) class Bar(models.Model): baz
I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I use django, and have a lengthy models.py file. class Foo(models.Model): name = models.CharField()
I have: class Foo(models.Model): pass class Bar(Foo): pass class Corn(Foo): pass # Now I
I have the following Django models: - class Company(models.Model): name = models.CharField(max_length=50) is_active =
I'm constantly doing the following pattern in Django: class MyModel(models.Model): FOO = 1 BAR
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
Let's assume we have following models: from django.db import models class Foo(models.Model): name =
In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories

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.