Class: QgsPolymorphicRelation

class qgis.core.QgsPolymorphicRelation

Bases: sip.wrapper

Default constructor. Creates an invalid relation.

QgsPolymorphicRelation(context: QgsRelationContext) Constructor with context. Creates an invalid relation.

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

A polymorphic relation consists of the same properties like a normal relation except for the referenced layer which is calculated based on one or several fields of the referencing 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, …

Methods

addFieldPair

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

createFromXml

Creates a relation from an XML structure.

fieldPairs

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

generateId

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

generateRelations

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

hasEqualDefinition

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

id

A (project-wide) unique id for this relation

isValid

Returns the validity of this relation.

layerRepresentation

Returns layer representation as evaluated string

name

Returns a human readable name for this relation.

referencedFields

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

referencedLayerExpression

Returns the expression to identify the parent layer

referencedLayerField

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

referencedLayerIds

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

referencingFields

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

referencingLayer

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

referencingLayerId

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

setId

Set an id for this relation

setName

Set a name for this relation

setReferencedLayerExpression

Sets the expression to identify the parent layer

setReferencedLayerField

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

setReferencedLayerIds

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

setReferencingLayer

Set the referencing (child) layer id.

setRelationStrength

Sets the relation strength for all the generated normal relations

strength

Returns the relation strength for all the generated normal relations

updateRelationStatus

Updates the validity status of this relation.

writeXml

Writes a relation to an XML structure.

Attributes

staticMetaObject

addFieldPair(self, referencingField: str, referencedField: str)

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 (str) – The field name on the referencing (child) layer (FK)

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

createFromXml(node: QDomNode, context: QgsReadWriteContext, relationContext: QgsRelationContext = QgsRelationContext())QgsPolymorphicRelation

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)

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

New in version 3.0.

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

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

A (project-wide) unique id for this relation

Return type

str

Returns

The id

isValid(self)bool

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)str

Returns layer representation as evaluated string

Parameters

layer (QgsVectorLayer) –

Return type

str

name(self)str

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)List[int]

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 (str) –

referencedLayerExpression(self)str

Returns the expression to identify the parent layer

Return type

str

referencedLayerField(self)str

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

Return type

str

referencedLayerIds(self)List[str]

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

Return type

List[str]

referencingFields(self)List[int]

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

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

Return type

QgsVectorLayer

Returns

The referencing layer

referencingLayerId(self)str

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)

Set an id for this relation

Parameters

id (str) –

setName(self, name: str)

Set a name for this relation

Parameters

name (str) –

setReferencedLayerExpression(self, expression: str)

Sets the expression to identify the parent layer

Parameters

expression (str) –

setReferencedLayerField(self, referencedLayerField: str)

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

Parameters

referencedLayerField (str) –

setReferencedLayerIds(self, childRelationIds: Iterable[str])

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

Parameters

childRelationIds (Iterable[str]) –

setReferencingLayer(self, id: str)

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

Parameters

id (str) –

setRelationStrength(self, relationStrength: QgsRelation.RelationStrength)

Sets the relation strength for all the generated normal relations

Parameters

relationStrength (QgsRelation.RelationStrength) –

staticMetaObject = <PyQt5.QtCore.QMetaObject object>
strength(self)QgsRelation.RelationStrength

Returns the relation strength for all the generated normal relations

Return type

QgsRelation.RelationStrength

updateRelationStatus(self)

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

writeXml(self, node: QDomNode, doc: QDomDocument)

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