Accessing Satchmo Product Variation Parent Attributes in Templates

Tue 15 Feb 2011 by in Web Design & Development

Tags: django python satchmo

While working on my first Satchmo project, I ran into a problem when I tried to add the product.short_description to the recently_added.html and best_sellers.html templates.


The Problem
My stuffs is missing...

The product variations don't have the product.short_description text! The product variation model is set up so each variation can have it's own description, price, weight, etc. After digging through the 2,400 line product.models module, I found:

def smart_attr(self, attr):
        """Retrieve an attribute, or its parent's attribute if it is null or blank.
        Ex: to get a weight.  obj.smart_attr('weight')"""
        ....
        return val

In a template, you can use the smart_attr template filter.

{% load satchmo_product %}
....
{% for product in page.object_list %}
    ....
    <p>{{ product|smart_attr:"short_description" }}</p>
    ....
{% endfor %}
....