Is it possible to, through Python, to create the relations that it's possible to create in Layers/Properties/Fields/Relations, and if so how? I first thought these were saved in the styles, but that was not the case.
Yes, that is possible:
rel = QgsRelation() rel.setReferencingLayer( 'child_layer_id' ) rel.setReferencedLayer( 'parent_layer_id' ) rel.addFieldPair( 'fk_field', 'pk_field' ) rel.setRelationId( 'my_relation' ) rel.setRelationName( 'Related Features' ) # rel.isValid() # It will only be added if it is valid. If not, check the ids and field names QgsProject.instance().relationManager().addRelation( rel )
For inclusion in the layer style, please open a feature request on the issue page.