Exemple #1
0
def get_attr(obj, attr):
    """
    Return class atributte.

    Example:
    {{ obj|get_attr:"my_atribute" }}

    """
    return get_value_by_relation_path(obj, attr)
Exemple #2
0
 def _get_component(self, model_type, field_path):
     # use cached components if already prefetched (using prefetch_related)
     # otherwise, perform regular SQL query
     try:
         components = self._prefetched_objects_cache['virtualcomponent_set']
     except (KeyError, AttributeError):
         return self.virtualcomponent_set.filter(
             model__type=model_type).values_list(field_path,
                                                 flat=True).first()
     else:
         for component in components:
             if component.model.type == model_type:
                 return get_value_by_relation_path(component, field_path)
         return None
 def _parse_item(self, item):
     """
     Get item fields values (using django __ (double-underscore) convention
     for related fields).
     """
     result = {}
     for f in self._invoice_report_item_fields:
         val = get_value_by_relation_path(item, f)
         # when it's function - call it! usefull for Choices
         # (get_<field_name>_display)
         if callable(val):
             val = val()
         elif isinstance(val, datetime.datetime):
             val = val.strftime(self._invoice_report_datetime_format)
         result[f] = str(val or self._invoice_report_empty_value)
     return result
 def autocomplete_tooltip(self):
     empty_element = '<empty>'
     tooltip = []
     for field in self.autocomplete_tooltip_fields:
         try:
             model_field = get_field_by_relation_path(self, field)
         except FieldDoesNotExist:
             logger.warning(
                 'Autocomplete tooltip field %s not found for %s', field,
                 self._meta.model_name)
             continue
         value = get_value_by_relation_path(self, field)
         if isinstance(model_field.choices, Choices):
             value = model_field.choices.name_from_id(value)
         label = str(model_field.verbose_name)
         if isinstance(value, Manager):
             value = ', '.join(map(str, value.all()))
         tooltip.append('{}: {}'.format(label.capitalize(), value
                                        or empty_element))
     return '\n'.join(tooltip)