MCPcopy Index your code
hub / github.com/tortoise/tortoise-orm / ManyToManyField

Function ManyToManyField

tortoise/fields/relational.py:586–654  ·  view source on GitHub ↗

ManyToMany relation field. This field represents a many-to-many between this model and another model. See :ref:`many_to_many` for usage information. You must provide the following: ``to``: The related model or name of the related model in a :samp:`'{app}.{model}'` fo

(
    to: type[Model] | str,
    through: str | None = None,
    forward_key: str | None = None,
    backward_key: str = "",
    related_name: str = "",
    on_delete: OnDelete = CASCADE,
    db_constraint: bool = True,
    unique: bool = True,
    **kwargs: Any,
)

Source from the content-addressed store, hash-verified

584
585
586def ManyToManyField(
587 to: type[Model] | str,
588 through: str | None = None,
589 forward_key: str | None = None,
590 backward_key: str = "",
591 related_name: str = "",
592 on_delete: OnDelete = CASCADE,
593 db_constraint: bool = True,
594 unique: bool = True,
595 **kwargs: Any,
596) -> ManyToManyRelation[Any]:
597 """
598 ManyToMany relation field.
599
600 This field represents a many-to-many between this model and another model.
601
602 See :ref:`many_to_many` for usage information.
603
604 You must provide the following:
605
606 ``to``:
607 The related model or name of the related model in a :samp:`'{app}.{model}'` format.
608
609 The following is optional:
610
611 ``through``:
612 The DB table that represents the through table.
613 The default is normally safe.
614 ``forward_key``:
615 The forward lookup key on the through table.
616 The default is normally safe.
617 ``backward_key``:
618 The backward lookup key on the through table.
619 The default is normally safe.
620 ``related_name``:
621 The attribute name on the related model to reverse resolve the many to many.
622 ``db_constraint``:
623 Controls whether or not a constraint should be created in the database for this foreign key.
624 The default is True, and that’s almost certainly what you want; setting this to False can be very bad for data integrity.
625 ``on_delete``:
626 One of:
627 ``field.CASCADE``:
628 Indicate that the model should be cascade deleted if related model gets deleted.
629 ``field.RESTRICT``:
630 Indicate that the related model delete will be restricted as long as a
631 foreign key points to it.
632 ``field.SET_NULL``:
633 Resets the field to NULL in case the related model gets deleted.
634 Can only be set if field has ``null=True`` set.
635 ``field.SET_DEFAULT``:
636 Resets the field to ``default`` value in case the related model gets deleted.
637 Can only be set is field has a ``default`` set.
638 ``field.NO_ACTION``:
639 Take no action.
640 ``unique``:
641 Controls whether or not a unique index should be created in the database to speed up select queries.
642 The default is True. If you want to allow repeat records, set this to False.
643 """

Callers 1

test_create_unique_indexFunction · 0.90

Calls 1

Tested by 1

test_create_unique_indexFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…