NDB에서 사용하는 Property Types
Property type | Description |
---|---|
IntegerProperty | 64-bit signed integer |
FloatProperty | Double-precision floating-point number |
BooleanProperty | Boolean |
StringProperty | Unicode string; up to 500 characters, indexed |
TextProperty | Unicode string; unlimited length, not indexed |
BlobProperty | Uninterpreted byte string: if you set indexed=True , up to 500 characters, indexed;if indexed is False (the default), unlimited length, not indexed.Optional keyword argument: compressed . |
DateTimeProperty | Date and time (see Date and Time Properties) |
DateProperty | Date (see Date and Time Properties) |
TimeProperty | Time (see Date and Time Properties) |
GeoPtProperty | Geographical location. This is a ndb.GeoPt object. The object has attributes lat and lon , both floats. You can construct one with two floats likendb.GeoPt(52.37, 4.88) or with a stringndb.GeoPt("52.37, 4.88") . (This is actually the same class as db.GeoPt ) |
KeyProperty | Datastore key Optional keyword argument: kind=kind, to require that keys assigned to this property always have the indicated kind. May be a string or a Model subclass. |
BlobKeyProperty | Blobstore key Corresponds to BlobReferenceProperty in the old db API, but the property value is a BlobKey instead of a BlobInfo ; you can construct a BlobInfo from it using BlobInfo(blobkey) |
UserProperty | User object. |
StructuredProperty | Includes one kind of model inside another, by value (see Structured Properties) |
LocalStructuredProperty | Like StructuredProperty , but on-disk representation is an opaque blob and is not indexed (see Structured Properties). Optional keyword argument: compressed . |
JsonProperty | Value is a Python object (such as a list or a dict or a string) that is serializable using Python's json module; the Datastore stores the JSON serialization as a blob. Unindexed by default. Optional keyword argument: compressed . |
PickleProperty | Value is a Python object (such as a list or a dict or a string) that is serializable using Python's pickle protocol; the Datastore stores the pickle serialization as a blob. Unindexed by default. Optional keyword argument: compressed . |
GenericProperty | Generic value Used mostly by the Expando class, but also usable explicitly. Its type may be any of int , long , float , bool , str , unicode , datetime ,Key , BlobKey , GeoPt , User , None . |
ComputedProperty | Value computed from other properties by a user-defined function. (See Computed Properties.) |
출처 : https://developers.google.com/appengine/docs/python/ndb/properties
Property가 가질수 있는 Options
Argument | Type | Default | Description |
---|---|---|---|
indexed | bool | UsuallyTrue | Include property in Datastore's indexes; if False , values cannot be queried but writes are faster. Not all property types support indexing; setting indexed to True fails for these.Unindexed properties cost fewer write ops than indexed properties. |
repeated | bool | False | Property value is a Python list containing values of the underlying type (see Repeated Properties). |
required | bool | False | Property must have a value specified. Cannot be combined with repeated=True but can be combined with default=True . |
default | Property's underlying type | None | Default value of property if none explicitly specified. Cannot be combined with repeated=True but can be combined with required=True . |
choices | List of values of underlying type | None | Optional list of allowable values. |
validator | Function | None | Optional function to validate and possibly coerce the value. Will be called with arguments (prop, value) and should either return the (possibly coerced) value or raise an exception. Calling the function again on a coerced value should not modify the value further. (For example, returning but not May also return See also Writing Property Subclasses |
verbose_name | string | None | Optional HTML label to use in web form frameworks like jinja2. |
출처 : https://developers.google.com/appengine/docs/python/ndb/properties
repeated (option)
복수의 데이터를 입력할때 사용된다. list 형태로 입력, 조회 된다.
class Article(ndb.Model): title = ndb.StringProperty() stars = ndb.IntegerProperty() tags = ndb.StringProperty(repeated=True) art = Article(title='Python versus Ruby', stars=3, tags=['python', 'ruby']) art.put()
뭐 이런식으로....
DateTimeProperty(DateProperty, TimeProperty)
DateTimeProperty는 date와 time 을 기록
DateProperty는 date만 기록
TimeProperty는 time만 기록
python 의 datetime 모듈로 매핑된다.
auto_now, auto_now_add 속성이 있는데
auto_now_add 속성을 True로 하면 데이터가 입력될 때만 현재시간 (UTC)을 입력하고
auto_now 속성을 True로 하면 입력, 수정 될 때마다 값이 자동으로 입력된다.
사용 예는
class Article(ndb.Model): title = ndb.StringProperty() stars = ndb.IntegerProperty() tags = ndb.StringProperty(repeated=True) date_time = ndb.DateTimeProperty(auto_now_add=True)
대충 위 처럼 사용하면 된다.
Structured Properties
string, int 이런 데이터 타입이 아닌 model 을 property로 사용 할 수 있다.
간단히 예를 보면
class Address(ndb.Model): type = ndb.StringProperty() street = ndb.StringProperty() city = ndb.StringProperty() class Contact(ndb.Model): name = ndb.StringProperty() addresses = ndb.StructuredProperty(Address, repeated=True) guido = Contact(name='Giudo', addresses=[Address(type='home', city='Amsterdam'), Address(type='work', street='Spear St', city='SF')]) guido.put()
위와 같이 Contact 모델이 Address 모델을 property로 가질 수 있다.
Computed Properties
property를 지정하지 않고 다른 property를 이용해서 property를 만드는 타입
class SomeEntity(ndb.Model): name = ndb.StringProperty() name_lower = ndb.ComputedProperty(lambda self: self.name.lower()) x = SomeEntity(name='Nick') #name = 'Nick' #name_lower = 'nick'
위와 같이 name property를 할당하면 이걸 가지고 name_lower을 만들어 낸다.
'Dev. > Google App Engine' 카테고리의 다른 글
Datastore - NDB API(Python) : Transaction (0) | 2013.09.25 |
---|---|
Datastore - NDB API(Python) : 데이터 조회 하기(Query) (0) | 2013.08.31 |
Datastore - NDB API(Python) : Model Hooks 사용하기 (trigger 역할) (0) | 2013.08.22 |
Datastore - NDB API(Python) : Entity의 구조를 정확히 모를때 (동적 Entity) (0) | 2013.08.21 |
Datastore - NDB API(Python) : Key를 이용한 데이터 삭제 (0) | 2013.08.16 |