You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/attributes.rst
+47-9Lines changed: 47 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -2,17 +2,19 @@ Custom Attributes
2
2
==========================
3
3
4
4
Attributes in PynamoDB are classes that are serialized to and from DynamoDB attributes. PynamoDB provides attribute classes
5
-
for all of the basic DynamoDB data types, as defined in the `DynamoDB documentation <http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataModel.html>`_.
5
+
for all DynamoDB data types, as defined in the `DynamoDB documentation <http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataModel.html>`_.
6
6
Higher level attribute types (internally stored as a DynamoDB data types) can be defined with PynamoDB. Two such types
7
-
are included with PynamoDB for convenience: `JSONAttribute` and `UnicodeDatetimeAttribute`.
7
+
are included with PynamoDB for convenience: ``JSONAttribute`` and ``UnicodeDatetimeAttribute``.
8
8
9
9
Attribute Methods
10
10
-----------------
11
11
12
-
All `Attribute` classes must define two methods, `serialize` and `deserialize`. The `serialize` method takes a Python
13
-
value and converts it into a format that can be stored into DynamoDB. The `deserialize` method takes a raw DynamoDB value
14
-
and converts it back into its value in Python. Additionally, a class attribute called `attr_type` is required for PynamoDB
15
-
to know which DynamoDB data type the attribute is stored as.
12
+
All ``Attribute`` classes must define three methods, ``serialize``, ``deserialize`` and ``get_value``. The ``serialize`` method takes a Python
13
+
value and converts it into a format that can be stored into DynamoDB. The ``get_value`` method reads the serialized value out of the DynamoDB record.
14
+
This raw value is then passed to the ``deserialize`` method. The ``deserialize`` method then converts it back into its value in Python.
15
+
Additionally, a class attribute called ``attr_type`` is required for PynamoDB to know which DynamoDB data type the attribute is stored as.
16
+
The ``get_value`` method is provided to help when migrating from one attribute type to another, specifically with the ``BooleanAttribute`` type.
17
+
If you're writing your own attribute and the ``attr_type`` has not changed you can simply use the base ``Attribute`` implementation of ``get_value``.
16
18
17
19
18
20
Writing your own attribute
@@ -45,8 +47,8 @@ Custom Attribute Example
45
47
------------------------
46
48
47
49
The example below shows how to write a custom attribute that will pickle a customized class. The attribute itself is stored
48
-
in DynamoDB as a binary attribute. The `pickle` module is used to serialize and deserialize the attribute. In this example,
49
-
it is not necessary to define `attr_type` because the `PickleAttribute` class is inheriting from `BinaryAttribute` which has
50
+
in DynamoDB as a binary attribute. The ``pickle`` module is used to serialize and deserialize the attribute. In this example,
51
+
it is not necessary to define ``attr_type`` because the ``PickleAttribute`` class is inheriting from ``BinaryAttribute`` which has
50
52
already defined it.
51
53
52
54
.. code-block:: python
@@ -107,4 +109,40 @@ Now we can use our custom attribute to round trip any object that can be pickled
107
109
108
110
>>>instance = CustomAttributeModel.get('red')
109
111
>>>print(instance.obj)
110
-
<Color: red>
112
+
<Color: red>
113
+
114
+
115
+
List Attributes
116
+
---------------
117
+
118
+
DynamoDB list attributes are simply lists of other attributes. DynamoDB asserts no requirements about the types embedded within the list.
119
+
DynamoDB is perfectly content with a list of ``UnicodeAttribute`` and ``NumberAttributes`` mixed together. Pynamo can provide type safety if it is required.
120
+
When defining your model use the ``of=`` kwarg and pass in a class. Pynamo will check that all items in the list are of the type you require.
121
+
122
+
.. code-block:: python
123
+
124
+
from pynamodb.attributes import ListAttribute, NumberAttribute, UnicodeAttribute
125
+
126
+
classOffice(Model):
127
+
classMeta:
128
+
table_name ='OfficeModel'
129
+
office_id = NumberAttribute(hash_key=True)
130
+
employees = ListAttribute(of=UnicodeAttribute)
131
+
132
+
133
+
Map Attributes
134
+
--------------
135
+
136
+
DynamoDB map attributes are objects embedded inside of top level models. See the examples `here <https://github.com/jlafon/PynamoDB/tree/devel/examples/office_model.py>`_.
137
+
When implementing your own MapAttribute you can simply extend ``MapAttribute`` and ignore writing serialization code.
138
+
These attributes can then be used inside of Model classes just like any other attribute.
139
+
140
+
.. code-block:: python
141
+
142
+
from pynamodb.attributes import MapAttribute, UnicodeAttribute
0 commit comments