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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:55:15+00:00 2026-06-12T19:55:15+00:00

I’m new to Python and Django, so please be patient with me. I have

  • 0

I’m new to Python and Django, so please be patient with me.

I have the following models:

class User(models.Model):
   name = models.CharField(max_length = 50)
   ...

class Post(models.Model):
   userBy = models.ForeignKey(User, related_name='post_user')
   userWall = models.ForeignKey(User, related_name='receive_user')
   timestamp = models.DateTimeField()
   post = models.TextField()

class Friend(models.Model):
   user1 = models.ForeignKey(User, related_name='request_user')
   user2 = models.ForeignKey(User, related_name='accept_user')
   isApproved = models.BooleanField()
   class Meta:
      unique_together = (('user1', 'user2'), )

I know that this may not be the best/easiest way to handle it with Django, but I learned it this way and I want to keep it like this.

Now, all I want to do is get all the post from one person and it’s friends. The question now is how to do it with the Django filters?

I think in SQL it would look something like this:

SELECT p.* FORM Post p, Friend f 
WHERE p.userBy=THEUSER OR (
    (f.user1=THEUSER AND f.user2=p.userBy) OR
    (f.user2=THEUSER AND f.user1=p.userBy)
)

With no guarantee of correctness, just to give an idea of the result I’m looking for.

  • 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-06-12T19:55:16+00:00Added an answer on June 12, 2026 at 7:55 pm
    from django.db.models import Q
    
    Post.objects.filter( \
        Q(userBy=some_user) | \
        Q(userBy__accept_user__user1=some_user) | \
        Q(userBy__request_user__user2=some_user)).distinct()
    

    UPDATE

    Sorry, that was my fault. I didn’t pay attention to your related_name values. See updated code above. Using userBy__accept_user or userBy__request_user alone won’t work because that’ll be a reference to Friend which you can’t compare to to User. What we’re doing here is following the reverse relationship to Friend and then once we’re there, seeing if the other user on the friend request is the user in question.

    This also illustrates the importance of describing reverse relationships appropriately. Many people make the same mistake you’ve made here and name the related_name after the model they’re creating the FK to (User), when actually, when we’re talking about reversing the FK, we’re now talking about Friend. Simply, your related names would make more sense as something like: friend_requests and accepted_friends.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

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.