I'm gonna try and simplify this as much as possible. Lets say i have the following:
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.
{% load people_extras %}
{% for person in people %}
{% show_person person %}
{% endfor %}
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.
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.
Take advantage of Django's contenttypes framework to identify your model types.
See this snippet: Child aware model inheritance and use Carl Meyer's suggestion in the comments (wrap the assignment in "if not self.id:
")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With