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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:16:53+00:00 2026-05-25T10:16:53+00:00

I want to do some unusual in Django orm. I have the model Car

  • 0

I want to do some unusual in Django orm.

I have the model Car. How can I extend it with additional information, separated by type, storing in another model?

For example, for entry of Car “My Truck”, which type is truck, i want to extend it with TruckInfo model.

Another entry “My Bus” i want to extend with BusInfo model.

In other words, i want to make a floating relationship.

It could be implemented by adding to Car column with type, and performing SELECT twice: 1) for selecting cars, 2) for selecting extra info using Car.Type field. But it is terrible solution. I want to make it in a single query.

Maybe you know solution in pure SQL, it will be useful too. Thx.

  • 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-25T10:16:53+00:00Added an answer on May 25, 2026 at 10:16 am

    It’s going to be pretty hard to give you a definitive answer without asking a lot more about your particular needs. However, there’s nothing in Django’s ORM that prevents you from doing this.

    Here’s a way to do it — note that I don’t by any means claim that it’s the only way, and I might recommend something else if given more clarification on your goals:

    class Automobile(models.Model):
        [...]
        type = models.ChoiceField(choices=(
            ('car', 'Car'),
            ('truck', 'Truck'),
            ('bus', 'Bus'),
        ))
    
        @property
        def detail(self):
            return getattr(self, self.type)
    
    class Car(Automobile):
        [...]
    
    class Truck(Automobile):
        [...]
    
    class Bus(Automobile):
        [...]
    

    Be sure that, if you go this route, you’ll want to read the documentation on multi-table inheritance: https://docs.djangoproject.com/en/1.3/topics/db/models/#multi-table-inheritance

    You also may or may not want the top-level model to be an actual table (see the text just above in the link I gave you for a discussion of abstract models). I can’t tell you which to use — it’s specific to what you’re trying to do.

    You’ll also probably want some custom signals that enforce data accuracy — for instance, to make sure you don’t save a Bus record for an automobile of type Truck.

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

Sidebar

Related Questions

I want some type of javascript to do like below I have a text
Ok I want some opinions how I can fix this mess of a method!
i am new in ssrs i want some sugeestiion from You can i use
I want some return about dom manipulation. I think we can save elements' status
I suppose to do some unusual stuff. I have a class template: template<class T>
I want some random no displaying on the web page and storing it into
I want some content to only be printed in my node-type.tpl.php if the node
I want some window to never receive mouse wheel up/downs, i can control this
I want to setup git for a somewhat unusual use case. I have a
I have an unusual problem. I have a function, operation in this function can

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.