I'm trying to find a way to easily loop through a Google App Engine (Python) Model and to print out each property and the property name.
This is how I'm trying it, and after some research I realize I can't next variables in each other. Is there a way to do this with filters or a way to return all of the values of the properties of a Model?
Thanks
templateargs = {'properties':MyModel.properties(), 'user1':MyModelObj, 'use开发者_C百科r2':MyModelObj}
{% for property in properties %}
Property: {{property}}
User1: {{user1.{{property}}}}
User2: {{user2.{{property}}}}
{% endfor %}
Take a look at how this to_json
method grabs all the property values on a Model instance, it should point you in the right direction.
After some more research I realized the proper way to do this was with gettattr. I used a filter along the lines of:
@register.filter
def mygetattr(obj, name):
return getattr(obj, name)
As suggested here
Try:
vars(user1)[property]
See Built-in Functions.html#vars for a short explanation of how vars
returns a dictionary of the instance variables.
精彩评论