Skip to content

ManyToMany class

edgy.ManyToManyField

Bases: ForeignKeyFieldFactory

_bases class-attribute instance-attribute

_bases = (BaseManyToManyForeignKeyField)

build_field classmethod

build_field(**kwargs)
PARAMETER DESCRIPTION
**kwargs

TYPE: Any DEFAULT: {}

Source code in edgy/core/db/fields/factories.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@classmethod
def build_field(cls, **kwargs: Any) -> BaseField:
    column_type = cls.get_column_type(**kwargs)
    pydantic_type = cls.get_pydantic_type(**kwargs)
    constraints = cls.get_constraints(**kwargs)
    default: None = kwargs.pop("default", None)
    server_default: None = kwargs.pop("server_default", None)

    new_field = cls._get_field_cls(cls)
    return new_field(  # type: ignore
        __type__=pydantic_type,
        annotation=pydantic_type,
        column_type=column_type,
        default=default,
        server_default=server_default,
        constraints=constraints,
        **kwargs,
    )

validate classmethod

validate(**kwargs)

default validation useful for one_to_one and foreign_key

PARAMETER DESCRIPTION
**kwargs

TYPE: Any DEFAULT: {}

Source code in edgy/core/db/fields/factories.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
@classmethod
def validate(cls, **kwargs: Any) -> None:
    """default validation useful for one_to_one and foreign_key"""
    on_delete = kwargs.get("on_delete", CASCADE)
    on_update = kwargs.get("on_update", RESTRICT)
    null = kwargs.get("null", False)

    if on_delete is None:
        raise FieldDefinitionError("on_delete must not be null.")

    if on_delete == SET_NULL and not null:
        raise FieldDefinitionError("When SET_NULL is enabled, null must be True.")

    if on_update and (on_update == SET_NULL and not null):
        raise FieldDefinitionError("When SET_NULL is enabled, null must be True.")
    related_name = kwargs.get("related_name", "")

    # tolerate None and False
    if related_name and not isinstance(related_name, str):
        raise FieldDefinitionError("related_name must be a string.")

get_constraints classmethod

get_constraints(**kwargs)

Returns the propery column type for the field, None for Metafields

PARAMETER DESCRIPTION
**kwargs

TYPE: Any DEFAULT: {}

Source code in edgy/core/db/fields/factories.py
56
57
58
59
@classmethod
def get_constraints(cls, **kwargs: Any) -> Sequence[Any]:
    """Returns the propery column type for the field, None for Metafields"""
    return []

get_column_type classmethod

get_column_type(**kwargs)

Returns the propery column type for the field, None for Metafields

PARAMETER DESCRIPTION
**kwargs

TYPE: Any DEFAULT: {}

Source code in edgy/core/db/fields/factories.py
61
62
63
64
@classmethod
def get_column_type(cls, **kwargs: Any) -> Any:
    """Returns the propery column type for the field, None for Metafields"""
    return None

get_pydantic_type classmethod

get_pydantic_type(**kwargs)

Returns the type for pydantic

PARAMETER DESCRIPTION
**kwargs

TYPE: Any DEFAULT: {}

Source code in edgy/core/db/fields/factories.py
66
67
68
69
@classmethod
def get_pydantic_type(cls, **kwargs: Any) -> Any:
    """Returns the type for pydantic"""
    return cls._type

_get_field_cls cached staticmethod

_get_field_cls()
Source code in edgy/core/db/fields/factories.py
71
72
73
74
@staticmethod
@lru_cache(None)
def _get_field_cls(cls: "FieldFactory") -> BaseField:
    return cast(BaseField, type(cls.__name__, cast(Any, cls._bases), {}))

__new__

__new__(to, *, through=None, from_fields=(), to_fields=(), **kwargs)
PARAMETER DESCRIPTION
to

TYPE: Union[Model, str]

through

TYPE: Optional[Model] DEFAULT: None

from_fields

TYPE: Sequence[str] DEFAULT: ()

to_fields

TYPE: Sequence[str] DEFAULT: ()

**kwargs

TYPE: Any DEFAULT: {}

Source code in edgy/core/db/fields/many_to_many.py
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
def __new__(  # type: ignore
    cls,
    to: Union["Model", str],
    *,
    through: Optional["Model"] = None,
    from_fields: Sequence[str] = (),
    to_fields: Sequence[str] = (),
    **kwargs: Any,
) -> BaseField:
    for argument in ["null", "on_delete", "on_update"]:
        if kwargs.get(argument, None):
            terminal.write_warning(f"Declaring `{argument}` on a ManyToMany relationship has no effect.")
    kwargs["null"] = True
    kwargs["on_delete"] = CASCADE
    kwargs["on_update"] = CASCADE

    return super().__new__(cls, to=to, through=through, **kwargs)