Is it possible to detect the type of a model property?
class MODULENAME(db.Model):
id1 = db.StringProperty()
id2 = db.StringProperty()
id3 = db.StringProperty()
property1 = db.StringProperty()
property2 = db.Strin开发者_Go百科gProperty()
createdate = db.DateProperty(auto_now_add=True)
changedate = db.DateProperty(auto_now_add=True)
isactive = db.BooleanProperty()
How do I test if "id3" is an int, float or a string?
I've learned so far that a model have a method called "_all_propertie" that returns a list with all the properties I've created in the model. Now I want to check for the property type so I can make the form automatically with te correct html input types and if I change the property type, the HTML will change automatically.
Does that make sense or am I too far off track?
/Michael
Working code:
from google.appengine.ext import db
class MODULENAME(db.Model):
id1 = db.StringProperty()
id2 = db.StringProperty()
id3 = db.DateProperty()
property1 = db.StringProperty()
createdate = db.DateProperty(auto_now_add=True)
changedate = db.DateProperty(auto_now_add=True)
isactive = db.BooleanProperty()
m = MODULENAME()
plist = m.properties()
for p in plist:
print "%s: %s" % (p, str(plist[p]))
Thanks, for the clues Daniel Roseman and Nick Johnson.
You don't want to use _all_properties
, since that just gives you the names of the fields as a set of strings.
Instead, you can use _properties
, which gives you a dictionary of field name to field type. Each of the field types, in turn, as a property called data_type
, which as the name implies is the type of the data it accepts.
So:
id3 = ModelName._properties['id3']
data_type = id3.data_type
Now data_type
contains type <int>
.
Try this:
for name, property in MyModel.properties().items():
print "Name: " + name + "; Type: " + str(property.data_type)
As Nick Johnson pointed out, _properties
and _all_properties
should not be used as they are internal and liable to change.
精彩评论