fill docstrings on mixins

This commit is contained in:
collerek
2020-12-31 11:52:05 +01:00
parent 101ea57879
commit c4ff69b683
6 changed files with 276 additions and 0 deletions

View File

@ -5,6 +5,10 @@ from ormar.fields.foreign_key import ForeignKeyField
class RelationMixin:
"""
Used to return relation fields/names etc. from given model
"""
if TYPE_CHECKING: # pragma no cover
from ormar import ModelMeta
@ -14,6 +18,12 @@ class RelationMixin:
@classmethod
def extract_db_own_fields(cls) -> Set:
"""
Returns only fields that are stored in the own database table, exclude all
related fields.
:return: set of model fields with relation fields excluded
:rtype: Set
"""
related_names = cls.extract_related_names()
self_fields = {
name for name in cls.Meta.model_fields.keys() if name not in related_names
@ -22,7 +32,13 @@ class RelationMixin:
@classmethod
def extract_related_fields(cls) -> List:
"""
Returns List of ormar Fields for all relations declared on a model.
List is cached in cls._related_fields for quicker access.
:return: list of related fields
:rtype: List
"""
if isinstance(cls._related_fields, List):
return cls._related_fields
@ -35,7 +51,13 @@ class RelationMixin:
@classmethod
def extract_related_names(cls) -> Set:
"""
Returns List of fields names for all relations declared on a model.
List is cached in cls._related_names for quicker access.
:return: list of related fields names
:rtype: List
"""
if isinstance(cls._related_names, Set):
return cls._related_names
@ -49,6 +71,12 @@ class RelationMixin:
@classmethod
def _extract_db_related_names(cls) -> Set:
"""
Returns only fields that are stored in the own database table, exclude
related fields that are not stored as foreign keys on given model.
:return: set of model fields with non fk relation fields excluded
:rtype: Set
"""
related_names = cls.extract_related_names()
related_names = {
name
@ -59,6 +87,17 @@ class RelationMixin:
@classmethod
def _exclude_related_names_not_required(cls, nested: bool = False) -> Set:
"""
Returns a set of non mandatory related models field names.
For a main model (not nested) only nullable related field names are returned,
for nested models all related models are returned.
:param nested: flag setting nested models (child of previous one, not main one)
:type nested: bool
:return: set of non mandatory related fields
:rtype: Set
"""
if nested:
return cls.extract_related_names()
related_names = cls.extract_related_names()