Skip to content
This repository

Use attrgetter if all other ways did not worked (in getitem) #9

Closed
wants to merge 1 commit into from

2 participants

Alexis Metaireau Armin Ronacher
Alexis Metaireau

This allow to support dotted attributes. You can ask for "obj.attribute", and this will work.
It is then possible to groupby on the sub attributes of the items, for instance.

Hope this helps.

Alexis Metaireau ametaireau Use attrgetter if all other ways did not worked. This allows to speci…
…fy dot

separated attributes while using the groupby builtin filter.
98453eb
Armin Ronacher
Owner

What exactly is this supposed to fix? Attributes are already looked up and for chaining attribute lookups you must call this function for each dot. Otherwise sub attributes could not be items.

Alexis Metaireau

This is supposed to allow the groupby to group by more precisely.
For instance, if I have articles that I want to group by year, I need to access item.date.year in the groupby filter.

Maybe that's not the way to go, and if not, please tell me what's the right way :)

When you're saying this function should be called for each dot, maybe can I split on . then and call this function recursively ?

Please let me know, this is a bit obscure by now.

Armin Ronacher
Owner

Can you open an issue for groupby not supporting more than one dot? That should be fixed there, not in the getattr function on the environment. Reasoning: this has specific requirements and must not change in a backwards incompatible way because people subclass this. Jinja2 already has the sandbox which also subclasses this.

Alexis Metaireau

okay, sure, thanks.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 20, 2010
Alexis Metaireau ametaireau Use attrgetter if all other ways did not worked. This allows to speci…
…fy dot

separated attributes while using the groupby builtin filter.
98453eb
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 5 additions and 1 deletion. Show diff stats Hide diff stats

  1. +5 1 jinja2/environment.py
6 jinja2/environment.py
@@ -10,6 +10,7 @@
10 10 """
11 11 import os
12 12 import sys
  13 +from operator import attrgetter
13 14 from jinja2 import nodes
14 15 from jinja2.defaults import *
15 16 from jinja2.lexer import get_lexer, TokenStream
@@ -361,7 +362,10 @@ def getitem(self, obj, argument):
361 362 try:
362 363 return getattr(obj, attr)
363 364 except AttributeError:
364   - pass
  365 + try:
  366 + return attrgetter(attr)(obj)
  367 + except AttributeError:
  368 + pass
365 369 return self.undefined(obj=obj, name=argument)
366 370
367 371 def getattr(self, obj, attribute):

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.