firstof as

206 views
Skip to first unread message

mail....@gmail.com

unread,
Nov 7, 2011, 2:57:37 PM11/7/11
to Django users
What I'm after is something like {% with firstof obj.thing
obj.get_something as thing %}

Is there any way to do this short of writing my own template tag and
figuring out which exists first? Seems like it would be a fairly
common need.

Nikolas Stevenson-Molnar

unread,
Nov 7, 2011, 4:50:48 PM11/7/11
to django...@googlegroups.com
Assuming obj is iterable, you can do: {% with
obj.0.thingobj.get_something as thing %}

...is that what you mean?

_Nik

mail....@gmail.com

unread,
Nov 7, 2011, 5:04:39 PM11/7/11
to Django users


On Nov 7, 3:50 pm, Nikolas Stevenson-Molnar <nik.mol...@consbio.org>
wrote:
> Assuming obj is iterable, you can do: {% with
> obj.0.thingobj.get_something as thing %}
>
> ...is that what you mean?

Not exactly, no. In my particular case, it's more of a check to see if
it's available, and if not, do a bit more work to get something that
will fill the current need. Or, in other words, maybe I'm storing it
locally, or maybe I'm not.

Another example:

{% with firstof obj.votes obj.get_votes as votes %}
Which would then let me do something with votes: "There have been
{{ votes }} vote{{ votes|pluralize }}.

Nikolas Stevenson-Molnar

unread,
Nov 7, 2011, 5:12:26 PM11/7/11
to django...@googlegroups.com
Ah, ok; I see what you want to do. I'm not aware of any way to do this,
other than as you mentioned a custom template tag.

Brett Epps

unread,
Nov 7, 2011, 5:30:16 PM11/7/11
to django...@googlegroups.com
Instead of adding a template tag, you could handle this in the class for
obj. Just have get_votes() cache its result and use the cached value
after the first time get_votes() is called. In the template, always call
get_votes().

Brett


On 11/7/11 4:12 PM, "Nikolas Stevenson-Molnar" <nik.m...@consbio.org>
wrote:

>--
>You received this message because you are subscribed to the Google Groups
>"Django users" group.
>To post to this group, send email to django...@googlegroups.com.
>To unsubscribe from this group, send email to
>django-users...@googlegroups.com.
>For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>

Reply all
Reply to author
Forward
0 new messages