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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:51:26+00:00 2026-05-28T02:51:26+00:00

I have a Django app with classes for different things such as Post, Quote,

  • 0

I have a Django app with classes for different things such as Post, Quote, Image, Link, etc. Like items in Tumblr. Each of these objects has a datetime.

I want to display a combined list of the 20 most recent items of all types. And I want to be able to page back through older items. ie, just like Tumblr’s pages.

But I can’t think how to do queries like this across many different types of object… Would I have to create a separate class, something like:

class CombinedItem(models.Model):
    date = models.DateTimeField()
    item_type = models.CharField() # eg, 'post', 'quote', 'image', 'link'
    item_id = models.IntegerField()

And add a new entry to that whenever I add a new Post, Quote, Image, etc. And then, for my list, query that CombinedItem class for the most recent items, and then fetch the main details for each item it returns? If so, what’s the most efficient way of doing all that? Thanks.

  • 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-28T02:51:27+00:00Added an answer on May 28, 2026 at 2:51 am

    Your best bet would be to actually work in reverse. Each of those is a type of user-generated content, so they should all inherit from a common base:

    class ContentItem(models.model):
        # common fields here
    
    class Post(ContentItem):
        # post specific fields
    
    ....
    

    Then, you can simply query the base model ContentItem and get a list of everything.

    The only potential issue is that the results are ContentItems and not Posts, Quotes, etc. However, in Django’s implementation of MTI (multi-table inheritance), the child class is an attribute on the parent class. So a “Post” ContentItem will have a .post attribute from which you can access the Post. You can combine this with hasattr to create utility methods that you can use in your view. For example:

    class ContentItem(models.Model):
        ...
        def is_post(self):
            return hasattr(self, 'post')
    

    Then, in your template:

    {% if content_item.is_post %}
        <!-- do some post-specific rendering here -->
    {% endif %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been developing some Django app and there's some duplicated code for different
I have a Django web app that I would like to use in a
I'm creating a little calendar app in Django. I have two model classes; Calendar
I have Django app that presents a list of items that you can add
I have two classes, Foo and Bar, in a Django app (on AppEngine if
I have python/django app on Heroku (Cedar stack) and would like to make it
I have a Django app that gets it's data completely from an external source
I have a Django app written in Python 2.5 and I plan to upgrade
I have a Django app that use a django-tagging. I need to port this
I have a Django app where most of the search is driven by foreign

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.