我没有解决这个问题的线索。
我有一个接收对象的模板标签:
{% score_for_object OBJECT_HERE as score2 %}
问题是我将来自原始选择的上下文传递给模板:
cursor = connection.cursor()
cursor.execute("select ...")
comments = utils.dictfetchall(cursor)
为了解决模板标签接受 Django 对象的问题,我写了一个模板标签:
'''
This template tag is used to transform a comment_id in an object to use in the django-voting app
'''
def retrive_comment_object(comment_id):
from myapp.apps.comments.models import MPTTComment
return MPTTComment.objects.get(id=comment_id)
有了这个模板标签,我希望它可以工作:
{% for item in comments %}
{% score_for_object item.comment_id|retrieve_comment_object as score2 %}
{{ score2.score }} {# expected to work, but not working #}
{% endfor %}
我的问题。可以从模板标签中检索对象吗?
此致,