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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:25:32+00:00 2026-05-30T02:25:32+00:00

I have a model Page , which can have Post s on it. What

  • 0

I have a model Page, which can have Posts on it. What I want to do is get every Page, plus the most recent Post on that page. If the Page has no Posts, I still want the page. (Sound familiar? This is a LEFT JOIN in SQL).

Here is what I currently have:

Page.objects.annotate(most_recent_post=Max('post__post_time'))

This only gets Pages, but it doesn’t get Posts. How can I get the Posts as well?

Models:

class Page(models.Model):
    name = models.CharField(max_length=50)
    created = models.DateTimeField(auto_now_add = True)
    enabled = models.BooleanField(default = True)

class Post(models.Model):
    user = models.ForeignKey(User)
    page = models.ForeignKey(Page)
    post_time = models.DateTimeField(auto_now_add = True)
  • 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-30T02:25:33+00:00Added an answer on May 30, 2026 at 2:25 am

    Depending on the relationship between the two, you should be able to follow the relationships quite easily, and increase performance by using select_related

    Taking this:

    class Page(models.Model):
        ...
    
    class Post(models.Model):
        page = ForeignKey(Page, ...)
    

    You can follow the forward relationship (i.e. get all the posts and their associated pages) efficiently using select_related:

    Post.objects.select_related('page').all()
    

    This will result in only one (larger) query where all the page objects are prefetched.

    In the reverse situation (like you have) where you want to get all pages and their associated posts, select_related won’t work. See this,this and this question for more information about what you can do.

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

Sidebar

Related Questions

I have a Post model which includes a date_published. I want to display links
I have a page that displays multiple Formsets, each of which has a prefix.
I have a page based on a model object, and I want to have
I have a JSF page that displays a RichFaces Treeview, from a TreeNodeImpl model
I have comment model that references (foreign key) a post model and there is
I have an aspx page which counts every visit and creates a cookie. But
I have a page which needs to be rendered in Quirks mode for the
I have a page that opens a modal dialog. After the operations done on
I have a page that displays a table in two different modes. In each
I have a website that uses one Action method which passes a pagename to

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.