A document returned from a map/reduce query. :param collection: An instance of :class:`~pymongo.Collection` :param key: Document/result key, often an instance of :class:`~bson.objectid.ObjectId`. If supplied as an ``ObjectId`` found in the given ``collection`
| 1105 | |
| 1106 | |
| 1107 | class MapReduceDocument: |
| 1108 | """A document returned from a map/reduce query. |
| 1109 | |
| 1110 | :param collection: An instance of :class:`~pymongo.Collection` |
| 1111 | :param key: Document/result key, often an instance of |
| 1112 | :class:`~bson.objectid.ObjectId`. If supplied as |
| 1113 | an ``ObjectId`` found in the given ``collection``, |
| 1114 | the object can be accessed via the ``object`` property. |
| 1115 | :param value: The result(s) for this key. |
| 1116 | """ |
| 1117 | |
| 1118 | def __init__(self, document, collection, key, value): |
| 1119 | self._document = document |
| 1120 | self._collection = collection |
| 1121 | self.key = key |
| 1122 | self.value = value |
| 1123 | |
| 1124 | @property |
| 1125 | def object(self): |
| 1126 | """Lazy-load the object referenced by ``self.key``. ``self.key`` |
| 1127 | should be the ``primary_key``. |
| 1128 | """ |
| 1129 | id_field = self._document()._meta["id_field"] |
| 1130 | id_field_type = type(id_field) |
| 1131 | |
| 1132 | if not isinstance(self.key, id_field_type): |
| 1133 | try: |
| 1134 | self.key = id_field_type(self.key) |
| 1135 | except Exception: |
| 1136 | raise Exception("Could not cast key as %s" % id_field_type.__name__) |
| 1137 | |
| 1138 | if not hasattr(self, "_key_object"): |
| 1139 | self._key_object = self._document.objects.with_id(self.key) |
| 1140 | return self._key_object |
| 1141 | return self._key_object |