私はDjangoにこのモデルを持っています:
News
Comments
Reactions
関係は次のとおりです。
a News has various Comments
a Comment has various Reactions
問題はユーザー(リクエスト/セッション中)です。ユーザーはリアクションまたはコメントをサブスクライブする可能性があります。彼はログインしているかどうかはわかりません。(これはfooの例であり、あまり意味がありません)
テンプレートではできません:
{% for reaction in this_news.comments.reactions %}
 {{ reaction.name }}
 {% if reaction.user_subscribed %} #reaction.user_subscribed(request.user)...
 You have subscribed this reaction!
 {% endif %}
{% endfor %}
問題は次のとおりです。
- テンプレート内のメソッドをパラメーターで呼び出すことができません(上記のコメントを参照)
 - モデルはリクエストにアクセスできません
 
今、私はModelのinit_userメソッドを呼び出して、リクエストを渡します。News次に、同じメソッドCommentとモデルを使用し、各モデルの子を循環するプロパティReactionを設定する必要があります。user_subscribed
これを行うためのよりスマートな方法はありませんか?
編集:カスタムタグの使用に関するIgnacioのヒントのおかげで、ユーザーを渡すために汎用モードを実行しようとしています(atmの使用方法がわからないため、クロージャの使用を避けています):
def inject_user(parser, token):
    try:
        # split_contents() knows not to split quoted strings.
        tag_name, method_injected, user = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires exactly three arguments" % token.contents.split()[0])
    return InjectUserNode(method_injected, user)
class InjectUserNode(template.Node):
    def __init__(self, method_injected, user):
        self.method_injected = template.Variable(method_injected)
        self.user = template.Variable(user)
    def render(self, context):
        try:
            method_injected = self.method_injected.resolve(context)
            user = self.user.resolve(context)
            return method_injected(user)
        except template.VariableDoesNotExist:
            return ''
私がそれを使うとき、私はで{% inject_user object.method_that_receives_a_user request.user %}このエラー'str' object is not callableにmethod_injected(user)出くわします; どうすれば修正できますか?