Return the ``Relation`` for ``name``, building it on first access. Relations are constructed lazily so that ``Model.__init__`` does not allocate a ``Relation`` (and, transitively, ``RelationProxy`` / ``QuerysetProxy``) for every declared FK on every instance — most
(self, name: str)
| 130 | parent._orm.remove(relation_name, item) |
| 131 | |
| 132 | def _get(self, name: str) -> Optional[Relation]: |
| 133 | """ |
| 134 | Return the ``Relation`` for ``name``, building it on first access. |
| 135 | |
| 136 | Relations are constructed lazily so that ``Model.__init__`` does |
| 137 | not allocate a ``Relation`` (and, transitively, ``RelationProxy`` / |
| 138 | ``QuerysetProxy``) for every declared FK on every instance — most |
| 139 | of which are never read on row-materialization paths. |
| 140 | |
| 141 | :param name: name of the relation |
| 142 | :type name: str |
| 143 | :return: existing or freshly constructed Relation, or None if the |
| 144 | name does not correspond to a declared relation |
| 145 | :rtype: ormar.relations.relation.Relation |
| 146 | """ |
| 147 | relation = self._relations.get(name) |
| 148 | if relation is not None: |
| 149 | return relation |
| 150 | field = self._field_map.get(name) |
| 151 | if field is None: |
| 152 | return None |
| 153 | relation = Relation( |
| 154 | manager=self, |
| 155 | type_=self._get_relation_type(field), |
| 156 | field_name=field.name, |
| 157 | to=field.to, |
| 158 | through=getattr(field, "through", None), |
| 159 | ) |
| 160 | self._relations[name] = relation |
| 161 | return relation |
| 162 | |
| 163 | def _get_relation_type(self, field: "BaseField") -> RelationType: |
| 164 | """ |