Class: QgsPolymorphicRelation

A relation where the referenced (parent) layer is calculated based on fields from the referencing (child) layer.

A polymorphic relation consists of the same properties like a normal relation except for the referenced (parent) layer which is calculated based on one or several fields of the referencing (child) layer.

In its most simple form, the referencing layer will just insert the layer name of the referenced layer into this field. To be more precise, a polymorphic relation is a set of normal relations having the same referencing layer but having the referenced layer dynamically defined. The polymorphic setting of the layer is solved by using an expression which has to match some properties of the the referenced layer like the table name, schema, uri, layer id, …

Added in version 3.18.

class qgis.core.QgsPolymorphicRelation[source]

Bases: object

__init__()

Default constructor. Creates an invalid relation.

__init__(context: QgsRelationContext)

Constructor with context. Creates an invalid relation.

Parameters:

context (QgsRelationContext)

__init__(other: QgsPolymorphicRelation)

Copies a relation. This makes a shallow copy, relations are implicitly shared and only duplicated when the copy is changed.

Parameters:

other (QgsPolymorphicRelation)

addFieldPair(self, referencingField: str | None, referencedField: str | None)[source]

Add a field pair which is part of this relation The first element of each pair are the field names of the foreign key. The second element of each pair are the field names of the matching primary key.

Parameters:
  • referencingField (Optional[str]) – The field name on the referencing (child) layer (FK)

  • referencedField (Optional[str]) – The field name on the referenced (parent) layer (PK)

static createFromXml(node: QDomNode, context: QgsReadWriteContext, relationContext: QgsRelationContext = QgsRelationContext()) QgsPolymorphicRelation[source]

Creates a relation from an XML structure. Used for reading .qgs projects.

Parameters:
  • node (QDomNode) – The dom node containing the relation information

  • context (QgsReadWriteContext) – to pass project translator

  • relationContext (QgsRelationContext = QgsRelationContext()) – a relation context

Return type:

QgsPolymorphicRelation

Returns:

A relation

fieldPairs(self) Dict[str, str]

Returns the field pairs which form this relation The first element of each pair are the field names of the foreign key. The second element of each pair are the field names of the matching primary key.

Return type:

Dict[str, str]

Returns:

The fields forming the relation

generateId(self)[source]

Generate a (project-wide) unique id for this relation

generateRelations(self) List[QgsRelation]

Returns a list of generated relations, based on the currently set referencedLayerIds()

Return type:

List[QgsRelation]

hasEqualDefinition(self, other: QgsPolymorphicRelation) bool[source]

Compares the two QgsRelation, ignoring the name and the ID.

Parameters:

other (QgsPolymorphicRelation) – The other relation

Return type:

bool

Returns:

True if they are similar

id(self) str[source]

A (project-wide) unique id for this relation

Return type:

str

Returns:

The id

isValid(self) bool[source]

Returns the validity of this relation. Don’t use the information if it’s not valid. A relation is considered valid if both referenced and referencig layers are valid.

Return type:

bool

Returns:

True if the relation is valid

layerRepresentation(self, layer: QgsVectorLayer | None) str[source]

Returns layer representation as evaluated string

Parameters:

layer (Optional[QgsVectorLayer])

Return type:

str

name(self) str[source]

Returns a human readable name for this relation. Mostly used as title for the children.

See also

id()

Return type:

str

Returns:

A name

referencedFields(self, layerId: str | None) List[int][source]

Returns a list of attributes used to form the referenced fields (most likely primary key) on the referenced (parent) layer.

Return type:

List[int]

Returns:

A list of attributes

Parameters:

layerId (Optional[str])

referencedLayerExpression(self) str[source]

Returns the expression to identify the parent layer

Return type:

str

referencedLayerField(self) str[source]

Returns the field in the referencing layer where the referenced layer identifier is stored

Return type:

str

referencedLayerIds(self) List[str][source]

Returns a list of layer ids to be used as potential referenced layers

Return type:

List[str]

referencingFields(self) List[int][source]

Returns a list of attributes used to form the referencing fields (foreign key) on the referencing (child) layer.

Return type:

List[int]

Returns:

A list of attributes

referencingLayer(self) QgsVectorLayer | None[source]

Access the referencing (child) layer This is the layer which has the field(s) which point to another layer

Return type:

Optional[QgsVectorLayer]

Returns:

The referencing layer

referencingLayerId(self) str[source]

Access the referencing (child) layer’s id This is the layer which has the field(s) which point to another layer

Return type:

str

Returns:

The id of the referencing layer

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

Set an id for this relation

Parameters:

id (Optional[str])

setName(self, name: str | None)[source]

Set a name for this relation

Parameters:

name (Optional[str])

setReferencedLayerExpression(self, expression: str | None)[source]

Sets the expression to identify the parent layer

Parameters:

expression (Optional[str])

setReferencedLayerField(self, referencedLayerField: str | None)[source]

Sets the field in the referencing layer where the referenced layer identifier is stored

Parameters:

referencedLayerField (Optional[str])

setReferencedLayerIds(self, childRelationIds: Iterable[str | None])[source]

Sets a list of layer ids to be used as potential referenced layers

Parameters:

childRelationIds (Iterable[Optional[str]])

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

Set the referencing (child) layer id. This layer will be searched in the registry.

Parameters:

id (Optional[str])

setRelationStrength(self, relationStrength: Qgis.RelationshipStrength)[source]

Sets the relation strength for all the generated normal relations

Parameters:

relationStrength (Qgis.RelationshipStrength)

strength(self) Qgis.RelationshipStrength[source]

Returns the relation strength for all the generated normal relations

Return type:

Qgis.RelationshipStrength

updateRelationStatus(self)[source]

Updates the validity status of this relation. Will be called internally whenever a member is changed.

writeXml(self, node: QDomNode, doc: QDomDocument)[source]

Writes a relation to an XML structure. Used for saving .qgs projects

Parameters:
  • node (QDomNode) – The parent node in which the relation will be created

  • doc (QDomDocument) – The document in which the relation will be saved