Class that keeps sets of fields to include, exclude, and flatten for a single model at a given alias/relation level. ``pk_only`` is a short-circuit consulted by :meth:`own_table_columns`: when set, only the primary-key column is projected for this model and the ``include``/``ex
| 238 | |
| 239 | @dataclass |
| 240 | class Excludable: |
| 241 | """ |
| 242 | Class that keeps sets of fields to include, exclude, and flatten for a single |
| 243 | model at a given alias/relation level. |
| 244 | |
| 245 | ``pk_only`` is a short-circuit consulted by :meth:`own_table_columns`: when |
| 246 | set, only the primary-key column is projected for this model and the |
| 247 | ``include``/``exclude`` sets are ignored. Producers should not combine |
| 248 | ``pk_only=True`` with a non-empty ``include``/``exclude`` - the flag wins. |
| 249 | """ |
| 250 | |
| 251 | include: set = field(default_factory=set) |
| 252 | exclude: set = field(default_factory=set) |
| 253 | flatten: set = field(default_factory=set) |
| 254 | pk_only: bool = False |
| 255 | |
| 256 | def get_copy(self) -> "Excludable": |
| 257 | """ |
| 258 | Return copy of self to avoid in place modifications. |
| 259 | |
| 260 | :return: copy of self with copied sets |
| 261 | :rtype: ormar.models.excludable.Excludable |
| 262 | """ |
| 263 | _copy = self.__class__() |
| 264 | _copy.include = {x for x in self.include} |
| 265 | _copy.exclude = {x for x in self.exclude} |
| 266 | _copy.flatten = {x for x in self.flatten} |
| 267 | _copy.pk_only = self.pk_only |
| 268 | return _copy |
| 269 | |
| 270 | def set_values(self, value: set, slot: Slot) -> None: |
| 271 | """ |
| 272 | Appends the data to the chosen slot (include/exclude/flatten). |
| 273 | |
| 274 | :param value: set of values to add |
| 275 | :type value: set |
| 276 | :param slot: which set to add the values to |
| 277 | :type slot: Slot |
| 278 | """ |
| 279 | current_value = getattr(self, slot) |
| 280 | current_value.update(value) |
| 281 | setattr(self, slot, current_value) |
| 282 | |
| 283 | def is_included(self, key: str) -> bool: |
| 284 | """ |
| 285 | Check if field in included (in set or set is {...}). |
| 286 | |
| 287 | :param key: key to check |
| 288 | :type key: str |
| 289 | :return: result of the check |
| 290 | :rtype: bool |
| 291 | """ |
| 292 | return (... in self.include or key in self.include) if self.include else True |
| 293 | |
| 294 | def is_explicitly_included(self, key: str) -> bool: |
| 295 | """ |
| 296 | Check whether ``key`` is explicitly named in the include set. |
| 297 |