Saturday, May 2, 2009

Never pass an extended db.Model to Django

Despite it will work locally, Django will not recognize a property added to a db.Model.

Example:

class Example(db.Model)
username = db.StringProperty()
password = db.StringProperty()

examples = Example().all().fetch(10)

for example in examples:
example.address = 'hello'

template_values = {
'examples': example
}

Django won't see example.address only with the local Appengine SDK environment! It might work with the db.Expando mode but quite confusing since the added property (address) will be unreachable only after the application is uploaded to the GAE servers.

Possible solution:
class Example(db.Model)
username = db.StringProperty()
password = db.StringProperty()

examples = Example().all().fetch(10)

elist = []
for example in examples:
eitem = {
'example': example,
'example_address': 'hello'
}
elist.append(eitem)

template_values = {
'examples': elist
}
Thanks for Eoghan for finding a solution.

No comments:

Post a Comment