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

  • Home
  • SEARCH
  • 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 251235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:34:40+00:00 2026-05-11T21:34:40+00:00

I’m gonna try and simplify this as much as possible. Lets say i have

  • 0

I’m gonna try and simplify this as much as possible. Lets say i have the following:

models.py

class Person(models.Model):
    name = models.CharField(max_length=255)

    def getRealPerson(self):
        # is there a better way to do this?
        ret = None
        try:
            ret = self.worker
        except:
            try:
                ret = self.retired
            except:
                ret = self
        return ret
class Worker(Person):
    salary = models.IntegerField(default=0)

class Retired(Person):
    age = models.IntegerField()

The example doesn’t really matter for what I want, just go with me here. The purpose of this is so I can have a master Person table to reference all people.

Ideally I want to be able to call a view of Person’s and have each ones specific details listed in a custom way for each class type. I’d like to use a custom inclusion_tag to do this.

people.html

{% load people_extras %}
{% for person in people %}
   {% show_person person %}
{% endfor %}

people_extras.py – templatetags

from django import template

@register.inclusion_tag('worker.html')
def show_worker(person):
    return {'person':person}

@register.inclusion_tag('worker.html')
def show_retired(person):
    return {'person':person}

#How do I write this function and use it as the show_person person tag?
from project.app.models import Worker, Retired
def show_person(person):
    person = person.getRealPerson():
    if isinstance(person, Worker):
        return show_worker # yes, this doesn't work.

I have no idea how to get it to call the correct template based on the person type.

I couldn’t figure out how to accomplish this with the template using {% ifequal %} like this:

{% ifequal person.getRealPerson.__class__.__name__ "Worker" %}
    {% show_worker %}
...

I went the route I wrote above with the templatetags. However, I dont know where to put the logic to determine the person type!

I think eventually I’d like to be able to use a generic view for this as well on the Person object.

If there’s a far better way to do this, I’m open to suggestions, I just want to get it working.

I’ve been kinda stuck here for over a day… could really use a push.

  • 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-11T21:34:40+00:00Added an answer on May 11, 2026 at 9:34 pm

    See this answer for an efficient way to get the right person type sub-object after querying on the Person table.

    Once you have that working, you should be able to eliminate most of the complexity in your template tags by using polymorphism. If you want to display each person type using a different template, make that template name a class attribute of the model, or even just make the template name based on the model name (using person._meta.module_name). One simple template tag should then be able to cover all cases without even knowing any details of which subclasses exist. EDIT This single tag can’t be registered using the inclusion_tag decorator, because you’ll need to determine the template name dynamically. But it’s easy to write using the simple_tag decorator:

    @register.simple_tag
    def show_person(person):
        t = template.loader.select_template(["%s.html" % person._meta.module_name,
                                             "person.html")
        return t.render({'person': person})
    

    This will render a Worker using worker.html, a Retired using retired.html, etc. If the specific template for the subtype is not found, it falls back to the default person.html.

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

Sidebar

Ask A Question

Stats

  • Questions 250k
  • Answers 250k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The run method for your class does not contain a… May 13, 2026 at 9:23 am
  • Editorial Team
    Editorial Team added an answer This two functions do two completely different things. MySql_Close() will… May 13, 2026 at 9:23 am
  • Editorial Team
    Editorial Team added an answer The navigator object contains all the info you need. This… May 13, 2026 at 9:23 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.