I will try to simplify this as much as possible. Suppose I have the following:
models.py
class Person(models.Model):
name = models.CharField(max_length=255)
def getRealPerson(self):
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 come with me here. The purpose of this is that I can have a table with the main character to refer to all people.
Ideally, I want to be able to call the Person view and each of them to specify individual data for each type of class. I would like to use a custom include_tag for 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}
from project.app.models import Worker, Retired
def show_person(person):
person = person.getRealPerson():
if isinstance(person, Worker):
return show_worker
I do not know how to make it invoke the correct template based on the type of person.
I could not figure out how to do this using the template, using {% ifequal%} as follows:
{% ifequal person.getRealPerson.__class__.__name__ "Worker" %}
{% show_worker %}
...
, , templatetags. , , , !
, Person.
, , , .
... .