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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:24:22+00:00 2026-05-12T21:24:22+00:00

First of all, some links to pages I’ve used for reference: A SO question

  • 0

First of all, some links to pages I’ve used for reference: A SO question, and the Django docs on generic relations and multi-table inheritance.

So far, I have a multi-table inheritance design set up. Objects (e.g: Car, Dog, Computer) can inherit an Item class. I need to be able to retrieve Items from the DB, get the subclass, and do stuff with it. My design doesn’t allow for retrieving the different kinds of objects one by one, so I need to use the Item container to wrap them all into one. Once I have the Item, the Django docs say I can get the subclass by referencing the attribute with the name of the model (e.g: myitem.car or myitem.computer).

I don’t know which type of object my item is referencing, so how can I get the child? Is there a built in way to do this? Here are some other ideas that I had: (some crazier than others)

  1. I was thinking I could add some sort
    of GenericForeignKey to Item that
    references the child, but I doubt it
    is even legal for a parent class to
    relate via a ForeignKey to a child
    class.
  2. I suppose I could have a
    ForeignKey(ContentType) in the Item
    class, and find the attribute of
    Item to get the child based on the
    ContentType’s name.
  3. Finally, although an ugly method, I might be able to keep a list of object types, and try each as an attribute until a DoesNotExist error is not thrown.

As you can see, these proposed solutions are not that elegant, but I’m hoping I won’t have to use one of them and someone here might have a better suggestion.

Thanks in advance

  • 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-12T21:24:22+00:00Added an answer on May 12, 2026 at 9:24 pm

    I have done something similar to method 2 in one of my projects:

    from django.db import models
    from django.contrib.contenttypes.models import ContentType
    
    class BaseModel(models.Model):
        type = models.ForeignKey(ContentType,editable=False)
        # other base fields here
    
        def save(self,force_insert=False,force_update=False):
            if self.type_id is None:
                self.type = ContentType.objects.get_for_model(self.__class__)
            super(BaseModel,self).save(force_insert,force_update)
    
        def get_instance(self):
            return self.type.get_object_for_this_type(id=self.id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I did some searching for an answer to this question...but I
first of all, my main question: What are some good troubleshooting techniques when you
first of all some details: I configured security as below in web.xml view plaincopy
First of all, I did some search and no answer on stackoverflow/google provided me
First of all thank you for your time and sorry if some issues are
First of all, apologize because I have seen some posts about this, but I
First of all, it needs to create some codes that handle any error in
First of all, sorry for my English. I need some help with this. Recently
First of all, I'm new to objective-c programming so I've probably made some mistakes
First of all, this is a homework assignment. I'm trying to make some bubbleSort

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.