0

I am trying to set up a debugging page with a list of objects, object property names, and object property values. I am trying to obtain the value of an particular property of a particular object type. Neither the object type or the property are know when I'm coding.

Here are the relevant sections I've got so for:

In my test.py

if self.request.get('objID'):
  qGet = self.request.get
  thisObj = db.get(db.Key(qGet('objID')))

template_values = { 'thisObj' : thisObj }

template = JINJA_ENVIRONMENT.get_template('objProp.html')
self.response.write(template.render(template_values))

and in my objProp.html template

{% if thisObj %}
  <ul>List of properties
  {% for p in thisObj.properties() %}
    <li>{{ p }} : {{ thisObj.p }}</li>
  {% endfor %}
  </ul>
{% endif %}

However since there is no property p in thisObj it always prints out a null value, really what I want so to print out the value of whatever property p is referring to at that particular point in the loop

Any help would be very much appreciated!

Mike PG
  • 231
  • 1
  • 4
  • 14

2 Answers2

1

This is an approach that I got working. I'm not going to accept it, because I'm not sold on it being a 'good' approach yet.

See also: Google App Engine: how can I programmatically access the properties of my Model class?

That question got me most of the way there, here is what I'm using and it appears to be functioning correctly:

{% if thisObj %}
  <ul>List of properties
  {% for p in thisObj.properties() %}
    <li>{{ p }} : {{ thisObj.properties()[p].get_value_for_datastore(thisObj) }}</li>
  {% endfor %}
  </ul>
{% endif %}

It seems like p is resolving as a string, thisObj.properties()[p] is returning the object property, then I just need to get the value out of that object with .get_value_for_datastore(thisObj)

Referenced from documentation here: The Property Class

Community
  • 1
  • 1
Mike PG
  • 231
  • 1
  • 4
  • 14
  • You could avoid the [p] step by using `thisObj.properties().values()` and then use `p.get_value_for_datastore(thisObj)` – Tim Hoffman May 07 '13 at 05:12
0

You should not use thisObj.p as you are not looking up the name in thisObj. .p in this instance is not the p from the loop, but trying to refer to a property or method called "p".

You should use

getattr(thisObj,p)

Tim Hoffman
  • 12,976
  • 1
  • 17
  • 29
  • I'm getting an indication that getattr() is undefined. Is there some way to enable this function in a Jinja template? – Mike PG May 07 '13 at 03:53
  • The other answer is the better one, and works. I don't use jinja so I don't know why don't have access to getattr – Tim Hoffman May 07 '13 at 05:10