Skip to content

relations.relation_proxy

RelationProxy Objects

1
class RelationProxy(Generic[T],  list)

Proxy of the Relation that is a list with special methods.

1
2
 | @property
 | related_field_name() -> str

On first access calculates the name of the related field, later stored in _related_field_name property.

Returns:

name of the related field :rtype: str

__getattribute__

1
 | __getattribute__(item: str) -> Any

Since some QuerySetProxy methods overwrite builtin list methods we catch calls to them and delegate it to QuerySetProxy instead.

Arguments:

  • item: name of attribute :type item: str

Returns:

value of attribute :rtype: Any

__getattr__

1
 | __getattr__(item: str) -> Any

Delegates calls for non existing attributes to QuerySetProxy.

Arguments:

  • item: name of attribute/method :type item: str

Returns:

method from QuerySetProxy if exists :rtype: method

_initialize_queryset

1
 | _initialize_queryset() -> None

Initializes the QuerySetProxy if not yet initialized.

_check_if_queryset_is_initialized

1
 | _check_if_queryset_is_initialized() -> bool

Checks if the QuerySetProxy is already set and ready.

Returns:

result of the check :rtype: bool

_check_if_model_saved

1
 | _check_if_model_saved() -> None

Verifies if the parent model of the relation has been already saved. Otherwise QuerySetProxy cannot filter by parent primary key.

_set_queryset

1
 | _set_queryset() -> "QuerySet[T]"

Creates new QuerySet with relation model and pre filters it with currents parent model primary key, so all queries by definition are already related to the parent model only, without need for user to filter them.

Returns:

initialized QuerySet :rtype: QuerySet

remove

1
 | async remove(item: "T", keep_reversed: bool = True) -> None

Removes the related from relation with parent.

Through models are automatically deleted for m2m relations.

For reverse FK relations keep_reversed flag marks if the reversed models should be kept or deleted from the database too (False means that models will be deleted, and not only removed from relation).

Arguments:

  • item: child to remove from relation :type item: Model
  • keep_reversed: flag if the reversed model should be kept or deleted too :type keep_reversed: bool

add

1
 | async add(item: "T", **kwargs: Any) -> None

Adds child model to relation.

For ManyToMany relations through instance is automatically created.

Arguments:

  • kwargs: dict of additional keyword arguments for through instance :type kwargs: Any
  • item: child to add to relation :type item: Model