Skip to content

Commit a7d0150

Browse files
authored
update ListAttribute readme (#282)
1 parent 0b3349d commit a7d0150

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

docs/attributes.rst

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,67 @@ List Attributes
116116
---------------
117117

118118
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.
119+
Creating an untyped list is done like so:
121120

122121
.. code-block:: python
123122
124123
from pynamodb.attributes import ListAttribute, NumberAttribute, UnicodeAttribute
125124
125+
class GroceryList(Model):
126+
class Meta:
127+
table_name = 'GroceryListModel'
128+
129+
store_name = UnicodeAttribute(hash_key=True)
130+
groceries = ListAttribute()
131+
132+
# Example usage:
133+
134+
GroceryList(store_name='Haight Street Market',
135+
groceries=['bread', 1, 'butter', 6, 'milk', 1])
136+
137+
PynamoDB can provide type safety if it is required. Currently PynamoDB does not allow type checks on anything other than ``MapAttribute`` and subclasses of ``MapAttribute``. We're working on adding more generic type checking in a future version.
138+
When defining your model use the ``of=`` kwarg and pass in a class. PynamoDB will check that all items in the list are of the type you require.
139+
140+
.. code-block:: python
141+
142+
from pynamodb.attributes import ListAttribute, NumberAttribute
143+
144+
145+
class OfficeEmployeeMap(MapAttribute):
146+
office_employee_id = NumberAttribute()
147+
person = UnicodeAttribute()
148+
149+
126150
class Office(Model):
127151
class Meta:
128152
table_name = 'OfficeModel'
129153
office_id = NumberAttribute(hash_key=True)
130-
employees = ListAttribute(of=UnicodeAttribute)
131-
154+
employees = ListAttribute(of=OfficeEmployeeMap)
155+
156+
# Example usage:
157+
158+
emp1 = OfficeEmployeeMap(
159+
office_employee_id=123,
160+
person='justin'
161+
)
162+
emp2 = OfficeEmployeeMap(
163+
office_employee_id=125,
164+
person='lita'
165+
)
166+
emp4 = OfficeEmployeeMap(
167+
office_employee_id=126,
168+
person='garrett'
169+
)
170+
171+
Office(
172+
office_id=3,
173+
employees=[emp1, emp2, emp3]
174+
).save() # persists
175+
176+
Office(
177+
office_id=3,
178+
employees=['justin', 'lita', 'garrett']
179+
).save() # raises ValueError
132180
133181
Map Attributes
134182
--------------

0 commit comments

Comments
 (0)