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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:06:01+00:00 2026-06-12T01:06:01+00:00

Suppose you have to model several classes that should have composite properties like dimensions

  • 0

Suppose you have to model several classes that should have composite properties like dimensions (width and height) or phone number (prefix, number and extension).

In Java (using JPA 2) I’d create a Dimensions class and annotate it with @Embeddable. This causes Dimension’s fields (e.g. width and height) to be embedded into every class that declares a property of type Dimensions.

How do you model these with Django while avoiding code duplication? It doesn’t make sense to create a separate Dimensions model and reference it with a ForeignKey field. And the classes do not have enough in common to justify model inheritance.

  • 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-06-12T01:06:02+00:00Added an answer on June 12, 2026 at 1:06 am

    I think you might be over-thinking inheritance. Inheritance is and is actually the recommended method to composite models. The following is an example of how properly use model inheritance in Django:

    class PhoneModelBase(model.Model):
        phone = models.CharField(max_length=16)
        ...
    
        class Meta:
            abstract = True
    
    class PhoneModel(PhoneModelBase):
        # phone is here without typing it
        # the only restriction is that you cannot redefine phone here
        # its a Django restriction, not Python restriction
        # phone = models.CharField(max_length=12) # <= This will raise exception
        pass
    

    So what this does is it creates a model PhoneModelBase, however the key here is that it uses class Meta with abstract=True.

    Here is more of behind the scenes of what is going on and some explanation of some of the Python concepts. I assume that you are not aware of them since you mentioned Java in the question. These Python concepts are actually rather confusing concepts so my explanation is probably not full, or even confusing, so if you will not follow, don’t mind. All you have to know is to use abstact = True. Here is the official doc: https://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes.

    Meta attribute within PhoneModelBase is just that, an attribute. It is the same as any other attribute within the class, except its a class instance (remember that in Python classes and functions are first order members). In addition, Python has this thing called __metaclass__ which you can add to you classes. __metaclass__ defines a way of how an instance of a class is build. More about that here. Django uses these in how it creates model class instances.

    So to create the PhoneModelBase class, the following is a rough outline:

    • When an instance of the PhoneModelBase class (the class itself, not instance of class – PhoneModelBase()) is being created, the __metaclass__ which comes from model.Model due to inheritance takes over the creation process
    • Within the __metaclass__, Python calls the function which creates the actual class instance and passes to it all of the fields from the class you are trying to create – PhoneModelBase. That will include phone, Meta and any other fields you define
    • It sees the Meta attribute and then it starts analyzing its attributes. According to the values of these attributes, Django will change the behavior of the model
    • It sees the abstract attribute and then changes the logic of the class its trying to create – PhoneModelBase by not storing it in db

    So then the PhoneModelBase, even though its definition looks very similar to a regular model, its not a regular model. It is just an abstract class which is meant to be used as composite in other models.

    When other models inherit from PhoneModelBase, their __metaclass__ will copy the attributes from the base model as if you manually typed those attributes. It will not be a foreign key on anything like that. All of the inherited attributes are going to be a part of the model and will be in the same table.

    Hopefully all of this makes some sense. If not, all you have to remember is just to use Meta class with abstract = True.

    EDIT

    As suggested in the comment, you can also inherit from multiple base classes. So you can have PhoneModelBase, DimensionsModelBase, and then you can inherit from both of these (or more), and all of the attributes from all base classes will be present in your model.

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

Sidebar

Related Questions

Suppose I have a method that combines several address fields like so: def address
Suppose I have a model Stock that has several StockPartition (its a property called
I've been wondering, suppose I have a model with an attribute that in every
Suppose I have an element (in my case, a StackPanel ) that contains several
I have a mvvm(model view viewmodel) silverlight application that has several views that need
Suppose I have a Post model and I'd like to run a method when
suppose I have a model and a view ,ths view have two method:one is
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose I have got a model ArticleVersion in my project which is defined as:
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a

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.