App Engine - why are there PhoneNumber, Link, Rating etc classes?

谁都会走 提交于 2019-11-28 07:16:32

问题


I haven't found any reason for the existence of a few of the App Engine classes. There's a PhoneNumber, a Link, a PostalAddress, a GeoPt, a Rating, etc. Why are these given special treatment? They don't seem to have any smarts - e.g. geo searching. I know Link has more space than a String property, but the rest?

See: http://code.google.com/appengine/docs/java/datastore/dataclasses.html


回答1:


Those types are 'semantic' types. They're present in the Java API for parity with the Python API. In the Python API, they define special behaviour with regards to the .to_xml() method - for example, a PhoneNumberProperty serializes like this:

<property name="foo" type="gd:phonenumber"><gd:phoneNumber>12345-678</gd:phoneNumber></property>



回答2:


I think they're mostly just there to cover common cases and save developers time. If a lot of apps use a phone number field, why require each developer to have to write them? A developer can still write their own if they need/want to.




回答3:


Not sure about java, but in python the following model/code (tested on dev server) will throw BadValueError, with the message "Invalid URL: stackoverflow.com"

class foo(db.model):
    link = db.LinkProperty()

bar = foo()
bar.link = 'stackoverflow.com'

While:

bar.link = 'http://stackoverflow.com'

Works fine.

I haven't tested, but the other properties may or may not also do validation.




回答4:


Basically using this types in your models allows to add indirect meta data to your code. This may be useful if you are working with any kind of universal renderer for your model classes or if you are performing validation of user input on your models.

For example if you are using PhoneNumber type for a field named userNumber you reflection based renderer may understand that it should automatically assign corresponding validator to text field which will represent it.

Regards, Pavel.



来源:https://stackoverflow.com/questions/1856126/app-engine-why-are-there-phonenumber-link-rating-etc-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!