Class: QgsRelationManager

Manages a set of relations between layers.

Class Hierarchy

Inheritance diagram of qgis.core.QgsRelationManager

Base classes

QObject

Methods

addPolymorphicRelation

Adds a new polymorphic relation.

addRelation

Add a relation.

clear

Remove any relation managed by this class.

context

Gets the relation context

polymorphicRelation

Returns the list of relations associated with a polymorphic relation

polymorphicRelations

Returns all the polymorphic relations

referencedRelations

Gets all relations where this layer is the referenced part (i.e. the parent table with the primary key being referenced from another layer).

referencingRelations

Gets all relations where the specified layer (and field) is the referencing part (i.e. the child table with the foreign key).

relation

Gets access to a relation by its id.

relations

Gets access to the relations managed by this class.

relationsByName

Returns a list of relations with matching names.

removePolymorphicRelation

Removes an existing polymorphic relation and it's generated relations.

removeRelation

Remove a relation.

setPolymorphicRelations

Sets the specified polymorphic relations and removes any polymorphic relations currently set.

setRelations

Will set the specified relations and remove any relation currently set.

updateRelationsStatus

Updates relations status

Static Methods

discoverRelations

Discover all the relations available from the current layers.

Signals

changed

Emitted when relations are added or removed to the manager.

relationsLoaded

Emitted when the relations were loaded after reading a project

class qgis.core.QgsRelationManager[source]

Bases: QObject

__init__(project: QgsProject | None = None)

Constructor for QgsRelationManager.

Parameters:

project (Optional[QgsProject] = None) – associated project (used to notify project of changes)

addPolymorphicRelation(self, polymorphicRelation: QgsPolymorphicRelation)[source]

Adds a new polymorphic relation. The generated relations are not available, they will be created automatically.

Parameters:

polymorphicRelation (QgsPolymorphicRelation)

addRelation(self, relation: QgsRelation)[source]

Add a relation. Invalid relations are added only if both referencing layer and referenced layer exist.

Parameters:

relation (QgsRelation) – The relation to add.

signal changed[source]

Emitted when relations are added or removed to the manager.

clear(self)[source]

Remove any relation managed by this class.

context(self) QgsRelationContext[source]

Gets the relation context

Return type:

QgsRelationContext

static discoverRelations(existingRelations: Iterable[QgsRelation], layers: Iterable[QgsVectorLayer]) List[QgsRelation]

Discover all the relations available from the current layers.

Parameters:
  • existingRelations (Iterable[QgsRelation]) – the existing relations to filter them out

  • layers (Iterable[QgsVectorLayer]) – the current layers

Return type:

List[QgsRelation]

Returns:

the list of discovered relations

polymorphicRelation(self, polymorphicRelationId: str | None) QgsPolymorphicRelation[source]

Returns the list of relations associated with a polymorphic relation

Parameters:

polymorphicRelationId (Optional[str])

Return type:

QgsPolymorphicRelation

polymorphicRelations(self) Dict[str, QgsPolymorphicRelation]

Returns all the polymorphic relations

Return type:

Dict[str, QgsPolymorphicRelation]

referencedRelations(self, layer: QgsVectorLayer | None = None) List[QgsRelation]

Gets all relations where this layer is the referenced part (i.e. the parent table with the primary key being referenced from another layer).

Parameters:

layer (Optional[QgsVectorLayer] = None) – The layer which should be searched for.

Return type:

List[QgsRelation]

Returns:

A list of relations where the specified layer is the referenced part.

referencingRelations(self, layer: QgsVectorLayer | None = None, fieldIdx: int = -2) List[QgsRelation]

Gets all relations where the specified layer (and field) is the referencing part (i.e. the child table with the foreign key).

Parameters:
  • layer (Optional[QgsVectorLayer] = None) – The layer which should be searched for.

  • fieldIdx (int = -2) – The field which should be part of the foreign key. If not set will return all relations.

Return type:

List[QgsRelation]

Returns:

A list of relations matching the given layer and fieldIdx.

relation(self, id: str | None) QgsRelation[source]

Gets access to a relation by its id.

Parameters:

id (Optional[str]) – The id to search for

Return type:

QgsRelation

Returns:

A relation. Invalid if not found.

relations(self) Dict[str, QgsRelation]

Gets access to the relations managed by this class.

Return type:

Dict[str, QgsRelation]

Returns:

A QMap where the key is the relation id, the value the relation object.

relationsByName(self, name: str | None) List[QgsRelation]

Returns a list of relations with matching names.

Parameters:

name (Optional[str]) – relation name to search for. Searching is case insensitive.

Return type:

List[QgsRelation]

Returns:

a list of matching relations

See also

relation()

signal relationsLoaded[source]

Emitted when the relations were loaded after reading a project

removePolymorphicRelation(self, polymorphicRelationId: str | None)[source]

Removes an existing polymorphic relation and it’s generated relations.

Parameters:

polymorphicRelationId (Optional[str])

removeRelation(self, id: str | None)[source]

Remove a relation.

Parameters:

id (Optional[str]) – The id of the relation to remove.

removeRelation(self, relation: QgsRelation)[source]

Remove a relation.

Parameters:

relation (QgsRelation) – The relation to remove.

setPolymorphicRelations(self, relations: Iterable[QgsPolymorphicRelation])[source]

Sets the specified polymorphic relations and removes any polymorphic relations currently set. Will remove any generated relations and recreate them.

Parameters:

relations (Iterable[QgsPolymorphicRelation])

setRelations(self, relations: Iterable[QgsRelation])[source]

Will set the specified relations and remove any relation currently set.

Parameters:

relations (Iterable[QgsRelation]) – A list of relations to set.

updateRelationsStatus(self)[source]

Updates relations status