72
I also had to write this code as a custom template tag recently. To handle all look-up scenarios, it first does a standard attribute look-up, then tries to do a dictionary look-up, then tries a getitem lookup (for lists to work), then follows standard Django template behavior when an object is not found.
(updated 2009-08-26 to now handle list index lookups as well)
# app/templatetags/getattribute.py
import re
from django import template
from django.conf import settings
numeric_test = re.compile("^\d+$")
register = template.Library()
def getattribute(value, arg):
"""Gets an attribute of an object dynamically from a string name"""
if hasattr(value, str(arg)):
return getattr(value, arg)
elif hasattr(value, 'has_key') and value.has_key(arg):
return value[arg]
elif numeric_test.match(str(arg)) and len(value) > int(arg):
return value[int(arg)]
else:
return settings.TEMPLATE_STRING_IF_INVALID
register.filter('getattribute', getattribute)
Template usage:
{% load getattribute %}
{{ object|getattribute:dynamic_string_var }}
3
I donβt think so. But it wouldnβt be too hard to write a custom template tag to return an attribute in the context dict. If youβre simply trying to return a string, try something like this:
class GetAttrNode(template.Node):
def __init__(self, attr_name):
self.attr_name = attr_name
def render(self, context):
try:
return context[self.attr_name]
except:
# (better yet, return an exception here)
return ''
@register.tag
def get_attr(parser, token):
return GetAttrNode(token)
Note that itβs probably just as easy to do this in your view instead of in the template, unless this is a condition that is repeated often in your data.
- [Django]-Django: Equivalent of "select [column name] from [tablename]"
- [Django]-Django bulk_create with ignore rows that cause IntegrityError?
- [Django]-Empty Label ChoiceField Django
2
I ended up adding a method to the model in question, and that method can be accessed like an attribute in the template.
Still, i think it would be great if a built in tag allowed you to dynamically lookup an attribute, since this is a problem a lot of us constantly have in our templates.
- [Django]-Django request get parameters
- [Django]-Unit testing with django-celery?
- [Django]-Save() prohibited to prevent data loss due to unsaved related object
2
Keeping the distinction between get and getattr,
@register.filter(name='get')
def get(o, index):
try:
return o[index]
except:
return settings.TEMPLATE_STRING_IF_INVALID
@register.filter(name='getattr')
def getattrfilter(o, attr):
try:
return getattr(o, attr)
except:
return settings.TEMPLATE_STRING_IF_INVALID
- [Django]-How do I match the question mark character in a Django URL?
- [Django]-How can one change the type of a Django model field from CharField to ForeignKey?
- [Django]-Django model manager objects.create where is the documentation?
1
That snippet saved my day but i needed it to span it over relations so I changed it to split the arg by β.β and recursively get the value.
It could be done in one line:
return getattribute(getattribute(value,str(arg).split(".")[0]),".".join(str(arg).split(".")[1:]))
but I left it in 4 for readability.
I hope someone has use for this.
import re
from django import template
from django.conf import settings
numeric_test = re.compile("^\d+$")
register = template.Library()
def getattribute(value, arg):
"""Gets an attribute of an object dynamically AND recursively from a string name"""
if "." in str(arg):
firstarg = str(arg).split(".")[0]
value = getattribute(value,firstarg)
arg = ".".join(str(arg).split(".")[1:])
return getattribute(value,arg)
if hasattr(value, str(arg)):
return getattr(value, arg)
elif hasattr(value, 'has_key') and value.has_key(arg):
return value[arg]
elif numeric_test.match(str(arg)) and len(value) > int(arg):
return value[int(arg)]
else:
#return settings.TEMPLATE_STRING_IF_INVALID
return 'no attr.' + str(arg) + 'for:' + str(value)
register.filter('getattribute', getattribute)
- [Django]-Django Rest Framework: turn on pagination on a ViewSet (like ModelViewSet pagination)
- [Django]-Organizing Django unit tests
- [Django]-Django: Switching to Jinja2?
- [Django]-Django ALLOWED_HOSTS IPs range
- [Django]-How to upgrade django?
- [Django]-Django in / not in query
0
I have developed a more generic solution based on @fotinakis solution, it serves to find the value of a string expression whether it is an attribute or a function, and also supports objects chaining.
import re
import types
numeric_test = re.compile("^\d+$")
register = template.Library()
def get_attr(object, arg):
if hasattr(object, str(arg)):
attr = getattr(object, arg)
if type(getattr(object, str(arg))) == types.MethodType:
return attr()
return attr
elif hasattr(object, 'has_key') and object.has_key(arg):
return object[arg]
elif numeric_test.match(str(arg)) and len(object) > int(arg):
return object[int(arg)]
else:
return object
@register.simple_tag(takes_context=True)
def get_by_name(context, name):
""""Get variable by string name {% get_by_name data_name.data_func... %}"""
print(context['instance'].get_edit_url())
arr = name.split('.')
obj = arr[0]
object = context[obj]
if len(arr) > 1:
for ar in arr:
object = get_attr(object, ar)
return object
- [Django]-How to change User representation in Django Admin when used as Foreign Key?
- [Django]-Why do we need to use rabbitmq
- [Django]-Which Stack-Overflow style Markdown (WMD) JavaScript editor should we use?