Fixes #94: Accessing app_label and model_name through content type rather than object.

As the object property is not always loaded by prefetch_related(), the `search_result_item` template tag will raise an AttributeError. But, as the object is essentially used for populating the string format params dict, it is possible to instead use the content type object (which appears to be available even when object is missing) for getting the `app_label` and `model_name` strings.

The only drawback is that the `obj` template variable may be `None` in some instances. It is not accessed in any standard tempaltes, and this issue would have caused an AttributeError earlier anyway.
This commit is contained in:
Simon Fransson 2015-02-10 15:54:00 +01:00
parent 3f2b0e00d8
commit 52794ac35d

View file

@ -24,14 +24,14 @@ def search_results(context, search_results):
return template.loader.render_to_string("watson/includes/search_results.html", context)
finally:
context.pop()
@register.simple_tag(takes_context=True)
def search_result_item(context, search_result):
obj = search_result.object
params = {
"app_label": obj._meta.app_label,
"model_name": obj.__class__.__name__.lower(),
"app_label": search_result.content_type.app_label,
"model_name": search_result.content_type.model,
}
# Render the template.
context.push()