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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:02:15+00:00 2026-05-26T09:02:15+00:00

I have a solution that I thought I could take care of model inheritance,

  • 0

I have a solution that I thought I could take care of model inheritance, but now looking at it again it doesn’t actually solve my problem. What I would like is to be able to call one model and then have the fields of the children model accessible to me. With inheritance I still have to put the child model name in the command line which defeats the whole purpose. Here is an example of what I would like:

class LessonModule(models.Model):
    lesson = models.ForeignKey('Lesson')
    name = models.CharField(max_length=100, blank=True)


class ProfileImage(LessonModule):
    file_loc = models.ImageField(upload_to="profiles/")
    detail_file_loc = models.ImageField(upload_to="profiles/", blank=True)

    def render(self):
        t = loader.get_template("template/profile_image.html")
        c = Context({'image_path': self.file_loc.url})
        return t.render(c)

    def __unicode__(self):
        return '[prof: %d]' % self.id

class Note(LessonModule):
    def __unicode__(self):
        return '[note: %d]' % self.id

    def render(self):
        return self.id

What I would like to be able to do is do a:

module = LessonModule.objects.get(pk=20)
module.render()

And have it run the corresponding render function. For example if the pk aligns with the Note model then it would just return the self.id. Of course this is simplified to what I want to do with these functions.

I don’t have to use Model Inheritance. It just looked like the best way of doing this. I just want a central area to look for all the possible modules.

I would also use this to pull all the LessonModules assigned to a Lesson from the lesson foreign key in the LessonModule.

  • 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-26T09:02:15+00:00Added an answer on May 26, 2026 at 9:02 am

    I haven’t used it, but this project looks like what you’re looking for:
    https://code.google.com/p/django-polymorphic-models/

    You can request LessonModule.objects.all() and then .downcast() each object automatically into a ProfileImage or a Note.

    Or add the PolymorphicMetaclass to your LessonModule to always retrieve ProfileImage and Note objects from a queryset.

    Note the cost of extra queries… polymorphism in Django models is done via table joins, not pure python code.

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

Sidebar

Related Questions

I have a solution that is being referenced by a MSBuild project. In the
I have a solution that I am trying to build in Visual Studio which
I have a solution that I work on with another person and the solution
I have a solution that contains both a Windows Forms application and a Console
Currently I have solution A that contains a domain layer base and solution B
I have a situation where we have a solution that uses Portable Areas and
In a nutshell, I have a solution that builds fine in the IDE, and
I am trying to decide which way to go. I have a solution that
I have a .NET solution that works together with some classic ASP pages, as
We have a reporting solution that works like this. User orders the report to

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.