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.