Saqib
Saqib

Reputation: 2480

Get All Field Values on Row

I am using the code below to get all values from a row.

from google.appengine.ext import db
from python_lib.models import Field

field = Field.all()[0]
names = Field.properties()

for key in names:
  print field.get(key)

But it giving the following error,

BadKeyError: Invalid string key name.

Upvotes: 1

Views: 431

Answers (2)

Tim Hoffman
Tim Hoffman

Reputation: 12986

You are confusing too different api's in this code.

for key in names:
   print field.get(key)

Your get() call is invalid, as your are actually trying to call a class method to fetch an entity from the datastore - see the docs for this method https://developers.google.com/appengine/docs/python/datastore/modelclass#Model_get .

To get a property by name from an instance of field (your object) you should be using getattr

as in

for key in names:
    print getattr(field,key)

or all alternately use get_value_for_datastore(model_instance) of the property object, which is returned from properties() call along with the name of the property.

Upvotes: 2

user784435
user784435

Reputation:

Is this what you want?

Code:

from google.appengine.ext import db

class Field(db.Model):
  author = db.StringProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)

a = Field(author="me")
a.put()

EDIT:

field = Field.all()
for f in field:
    print f.author
    print f.content


props = Field.properties()

print props
for vals in props:
  print vals

output:

<__main__.Field object at 0xb8c9e4c>
{'content': <google.appengine.ext.db.StringProperty object at 0xb7c0aec>, 'date':     <google.appengine.ext.db.DateTimeProperty object at 0xb7c06cc>, 'author':     <google.appengine.ext.db.StringProperty object at 0xb7c0f6c>}
content
date
author

Upvotes: 0

Related Questions