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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:30:34+00:00 2026-05-13T10:30:34+00:00

I have two models than inherited from the same abstract base class. I would

  • 0

I have two models than inherited from the same abstract base class.

I would expect to be able to get all instances from classes that are children of the base class with something like AbstractClass.objects.all()

Of course I could join queries on all children but that’s awful and it stops working if I add new children class.

Is this possible with Django ORM ? What would be the elegant solution ?

  • 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-13T10:30:34+00:00Added an answer on May 13, 2026 at 10:30 am

    I have used django’s other inheritance methods because I hit the same problem you are running into. I’m not sure if there is an elegant solution. Ultimately, you need several queries done on the DB and for the results to be merged together. I can’t picture the ORM supporting that.

    Here is my usual hackish approach for this situation:

    class NotQuiteAbstractBaseClass(models.Model):
        def get_specific_subclass(self):
            if self.model1:
                return self.model1
            elif self.model2:
                return self.model2
            else:
                raise RuntimeError("Unknown subclass")
    
    class Model1(NotQuiteAbstractBaseClass):
        def whoami(self):
            return "I am a model1"
    
    class Model2(NotQuiteAbstractBaseClass):
        def whoami(self):
            return "I am a model2"
    

    Then, you can query the entire list like this:

    for obj in NotQuiteAbstractBaseClass.objects.iterator():
        obj = obj.get_specific_subclass()
        print obj.whoami()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Article and Post that both inherit from a base model
I have two models. order and line_item. class Order < ActiveRecord::Base has_many :line_items has_many
I have two models: class Member < ActiveRecord::Base has_many :member_tags end and class MemberTag
I have two models nested with accepts_nested_attributes_for : class Place < ActiveRecord::Base # Relations..
I have two models: Company and Person class Person < ActiveRecord::Base belongs_to :company end
So I have two models here: class Screen < ActiveRecord::Base belongs_to :user validates :screen_size,
I have two models, A and B, and one light, L. I would like
I have two models, Post hasMany Comment . How do I select all Post
I have two models class Employer(models.Model): name = models.CharField(max_length=300, blank=False) id = models.IntegerField() status
I have two tables. In one table we enter all types of Models, each

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.