Skip to content

through_field

ThroughField

Bases: ForeignKeyField

Field class used to access ManyToMany model through model.

Source code in ormar/fields/through_field.py
Python
68
69
70
71
class ThroughField(ForeignKeyField):  # type: ignore[misc]
    """
    Field class used to access ManyToMany model through model.
    """

Through(to, *, name=None, related_name=None, **kwargs)

Despite a name it's a function that returns constructed ThroughField. It's a special field populated only for m2m relations. Accepts number of relation setting parameters as well as all BaseField ones.

:param to: target related ormar Model :type to: Model class :param name: name of the database field - later called alias :type name: str :param related_name: name of reversed FK relation populated for you on to model :type related_name: str It is for reversed FK and auto generated FK on through model in Many2Many relations. :param kwargs: all other args to be populated by BaseField :type kwargs: Any :return: ormar ForeignKeyField with relation to selected model :rtype: ForeignKeyField

Source code in ormar/fields/through_field.py
Python
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def Through(  # noqa CFQ002
    to: "ToType",
    *,
    name: Optional[str] = None,
    related_name: Optional[str] = None,
    **kwargs: Any,
) -> Any:
    """
    Despite a name it's a function that returns constructed ThroughField.
    It's a special field populated only for m2m relations.
    Accepts number of relation setting parameters as well as all BaseField ones.

    :param to: target related ormar Model
    :type to: Model class
    :param name: name of the database field - later called alias
    :type name: str
    :param related_name: name of reversed FK relation populated for you on to model
    :type related_name: str
    It is for reversed FK and auto generated FK on through model in Many2Many relations.
    :param kwargs: all other args to be populated by BaseField
    :type kwargs: Any
    :return: ormar ForeignKeyField with relation to selected model
    :rtype: ForeignKeyField
    """
    nullable = kwargs.pop("nullable", False)
    owner = kwargs.pop("owner", None)
    namespace = dict(
        __type__=to,
        to=to,
        through=None,
        alias=name,
        name=kwargs.pop("real_name", None),
        related_name=related_name,
        virtual=True,
        owner=owner,
        nullable=nullable,
        unique=False,
        column_type=None,
        primary_key=False,
        index=False,
        default=None,
        server_default=None,
        is_relation=True,
        is_through=True,
    )

    Field = type("Through", (ThroughField, BaseField), {})
    return Field(**namespace)