I have layers loaded from PostGIS into QGIS(2.14). Since it is a database and I try to respect the normalization rules, datas are not always stored directly into the layer table, and sometimes they exist as an ID number. With the "value relation" setting in the field properties I can display the corresponding information as a drop-down menu. But I also would like to display this information as a label into the map, for now I just can display the ID number.

Does anyone have any idea on how to display the actual information ? I can think of few workaround, of which none are entirely satisfying :

  • adding a field with the information into the DB table, but in order to keep a bit of DB logic I would need to set some triggers automatically updating the corresponding fields between tables. In other words, a bit painful for a rather simple process, but doable.
  • using a QGIS table join and display the joined data, but it seems not possible to save the table join like as a default style which would be automatically present everytime I load my layer. If it could work, I am developping a process meant to be used by many users, which nonetheless are not having any QGIS experience for now, but the idea is to automatize things and this is typically the kind of little things that are generally adding to one another and complexify a process. So I'd avoid that.

2 Answers 2


If you can display the dropdown, you should also be able to display the corresponding labels. If you id number is listed like (1), (2), ... and not showing the value text, the value relation widget is probably not set after layer loading. I quess that you use default styles on your layer saved in the database. The default style will not set layer, key column and value column of the widget.

I read somewhere you could use:


If you read the style from the layerstyle table in PostGIS. If did not work 100% for me.

I explicitly set the widget properties from code like:

fieldIndex = vlayer.fieldNameIndex(list_id_field)
layer_id = QgsMapLayerRegistry.instance().mapLayersByName(list_tabname)[0].id().encode('cp1252')
            vlayer.setEditorWidgetV2(fieldIndex, 'ValueRelation')
vlayer.setEditorWidgetV2Config(fieldIndex, {'Layer': '{}'.format(layer_id), 'Key': list_id_field, 'Value': list_value_column, 'allow_null': False})

The above code does work. Use the encoding fitting you data. It would be nice if the default style, could load the entire widget setting. Remember also the relation list table also has to be open in QGIS, for the widget to load. Later this week I will check up on the applyNamedStyle method. Another applyNamedStyle can be found here:

How to load a style from POSTGIS in PyQt

  • I'm sorry, I have no idea what to do with that. Can I include that in the XML corresponding of my layer_style table in PostGIS ? If yes could you give me some direction on how to include a python piece of code in an XML ? Commented Mar 28, 2017 at 6:23
  • The value relation widget does not work with default styles. Either you need to open the table via a previous saved qgis project file, where the widget settings is saved or you need to set the style manually with some piece of Python code after load.
    – Jakob
    Commented Mar 29, 2017 at 6:39
  • It seems to me the value relation widget works with default style. Well if I understand properly what you mean. I saved a layer style in my DB for a layer including a field "value related", and on opening it is like it was when I saved it. Maybe it wasn't in older versions, but in mine (2.14) it does work like that. Commented Mar 29, 2017 at 7:59
  • If it works with default styles you should have your labels show with values (texts)and not keys (numbers)?
    – Jakob
    Commented Mar 29, 2017 at 8:24
  • I do not. I may have not made myself clear in my original question. I didn't find how to display values from value relation as labels. In the label parameter window, when I edit the expression to be displayed, I only can pick the key value. But I may have found another way to work, which is to work with views instead of directly with tables, and it could solve this problem. Commented Mar 29, 2017 at 8:58

Finally I didn't manage to find a solution directly to this situation, I instead organized my work differently, and instead of displaying raw tables with filters and value relations in QGIS, I created views with rules for the updates of the database. And I just import these views in QGIS, views which already include the datas as I want them (since I formatted them while creating the view). Furthermore it also allows me with a little scripting to only display the datas in the area concerned by the current project, and not all the territory, gaining in RAM and fluidity of display.

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.