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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:55:30+00:00 2026-05-26T09:55:30+00:00

I have multiple models like this: class Model1(models.Model): user = models.ForeignKey(User) model1_filed1 = models.CharField()

  • 0

I have multiple models like this:

class Model1(models.Model):
    user = models.ForeignKey(User)
    model1_filed1 = models.CharField()


class Model2(models.Model):
    user = models.ForeignKey(User)
    model2_filed1 = models.CharField()

....

As you can see all the models have a filed user = models.ForeignKey(User) on each model so when I save my model data I know what user saved it.

On my project I need to:

  1. Do a search on model1_filed1 (Moldel1). Something like find all the records from Model1 that have model1_filed1 containing the string “foo”.

  2. Once I find Model1 records containing the tring “foo” on the mode1_filed1 join all the records from Model2 that have the same user id as the Model1 records just found.

Any ides?

  • 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-26T09:55:31+00:00Added an answer on May 26, 2026 at 9:55 am
    # 1
    m1 = Model1.objects.filter(model1_filed1__contains='foo')
    
    # 2
    m2 = Model2.objects.filter(user__in=[x.user_id for x in m1])
    
    # or one-liner for # 2, may be heavier for DB than two requests
    m2 = Model2.objects.filter(user__model1__model1_filed1__contains='foo')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I've defined this model: class Identifier(models.Model): user = models.ForeignKey(User) key = models.CharField(max_length=64)
My django model looks like this: class Entity(models.Model): name = models.CharField(max_length=40) examples = models.ManyToManyField(Example,
When I have a given django model class like this: class BaseClass(models.Model): some_field =
In my model I have an abstract User class, and multiple subclasses such as
in my application, I have a User model. Each user can have multiple (email)
I have the following in my models.py: class HostData(models.Model): Manager = models.ForeignKey(Managers) Host =
I have this model: class Auth(models.Model): TYPES = ( ('agent', 'Agent'), ('broker', 'Broker'), )
My ability model looks something like this: class Ability include CanCan::Ability def initialize(user) if
I have a model that looks like this: public class Book { public string
I would like to have one model (event) that has multiple polymorphic HABTM associations

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.