I've setup an edit widget for a field as a relation reference like shown below and save the configuration as a QGIS layer style file (*.qml):

enter image description here

With PyQGIS I do the following steps

  • load the vector layer necessary for the relations
  • create the relations
  • load the style files

But after this, the expression dropdown is empty because it is not stored in the qml file:

<edittype widgetv2type="RelationReference" name="nv_bezeichnung">
   <widgetv2config OrderByValue="0" fieldEditable="1" ShowForm="0" Relation="rv-nv" ReadOnly="0" MapIdentification="1" labelOnTop="1" AllowNULL="0"/>

The edit widgets can be configured with PyQGIS like

layer.setEditorWidgetV2Config(fieldId, {'key':'value'})

with keys according to the xml snippet above, but there seems to be no key to store the expression in, therefore my question:

How to store or save the 'Display Expression' in a relation reference or set it up using PyQGIS?

1 Answer 1


This problem has quite a simple solution, which is not so obvious at first sight. I found it by studying the documentation to QgsVectorLayer in detail. This has a method setDisplayExpression(). The solution is that the display expression is not a property of the child layer for which the relation reference is set up, but for it's parent layer (the 1 in the 1:n relation).

So in PyQGIS the Display expression for the child layer Rohrverbund is set up far its parent layer Netzverteiler via


where attribute bezeichnung appears in the combo box where parent layer is selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.