Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango model inheritance w/ custom inclusion_tags
    text
    copied!<p>I'm gonna try and simplify this as much as possible. Lets say i have the following:</p> <h1>models.py</h1> <pre><code>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() </code></pre> <p>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.</p> <p>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.</p> <h1>people.html</h1> <pre><code>{% load people_extras %} {% for person in people %} {% show_person person %} {% endfor %} </code></pre> <h1>people_extras.py - templatetags</h1> <pre><code>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. </code></pre> <p>I have no idea how to get it to call the correct template based on the person type.</p> <p>I couldn't figure out how to accomplish this with the template using {% ifequal %} like this:</p> <pre><code>{% ifequal person.getRealPerson.__class__.__name__ "Worker" %} {% show_worker %} ... </code></pre> <p>I went the route I wrote above with the templatetags. However, I dont know where to put the logic to determine the person type!</p> <p>I think eventually I'd like to be able to use a generic view for this as well on the Person object.</p> <p>If there's a far better way to do this, I'm open to suggestions, I just want to get it working.</p> <p>I've been kinda stuck here for over a day... could really use a push.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload