A dictionary-based collection type with attribute-based keying. .. versionchanged:: 2.0 Renamed :data:`.attribute_mapped_collection` to :func:`.attribute_keyed_dict`. Returns a :class:`.KeyFuncDict` factory which will produce new dictionary keys based on the value of a particula
(
attr_name: str, *, ignore_unpopulated_attribute: bool = False
)
| 225 | |
| 226 | |
| 227 | def attribute_keyed_dict( |
| 228 | attr_name: str, *, ignore_unpopulated_attribute: bool = False |
| 229 | ) -> Type[KeyFuncDict[Any, Any]]: |
| 230 | """A dictionary-based collection type with attribute-based keying. |
| 231 | |
| 232 | .. versionchanged:: 2.0 Renamed :data:`.attribute_mapped_collection` to |
| 233 | :func:`.attribute_keyed_dict`. |
| 234 | |
| 235 | Returns a :class:`.KeyFuncDict` factory which will produce new |
| 236 | dictionary keys based on the value of a particular named attribute on |
| 237 | ORM mapped instances to be added to the dictionary. |
| 238 | |
| 239 | .. note:: the value of the target attribute must be assigned with its |
| 240 | value at the time that the object is being added to the |
| 241 | dictionary collection. Additionally, changes to the key attribute |
| 242 | are **not tracked**, which means the key in the dictionary is not |
| 243 | automatically synchronized with the key value on the target object |
| 244 | itself. See :ref:`key_collections_mutations` for further details. |
| 245 | |
| 246 | .. seealso:: |
| 247 | |
| 248 | :ref:`orm_dictionary_collection` - background on use |
| 249 | |
| 250 | :param attr_name: string name of an ORM-mapped attribute |
| 251 | on the mapped class, the value of which on a particular instance |
| 252 | is to be used as the key for a new dictionary entry for that instance. |
| 253 | :param ignore_unpopulated_attribute: if True, and the target attribute |
| 254 | on an object is not populated at all, the operation will be silently |
| 255 | skipped. By default, an error is raised. |
| 256 | |
| 257 | .. versionadded:: 2.0 an error is raised by default if the attribute |
| 258 | being used for the dictionary key is determined that it was never |
| 259 | populated with any value. The |
| 260 | :paramref:`_orm.attribute_keyed_dict.ignore_unpopulated_attribute` |
| 261 | parameter may be set which will instead indicate that this condition |
| 262 | should be ignored, and the append operation silently skipped. |
| 263 | This is in contrast to the behavior of the 1.x series which would |
| 264 | erroneously populate the value in the dictionary with an arbitrary key |
| 265 | value of ``None``. |
| 266 | |
| 267 | |
| 268 | """ |
| 269 | |
| 270 | return _mapped_collection_cls( |
| 271 | _AttrGetter(attr_name), |
| 272 | ignore_unpopulated_attribute=ignore_unpopulated_attribute, |
| 273 | ) |
| 274 | |
| 275 | |
| 276 | def keyfunc_mapping( |