I am trying to set the edit type of a vector layer to use a value relation. The Python commands below are processed without errors or warnings, but the when I check the properties in QGIS, only the EditType is set, but the data settings are empty. I use:

vlayer.setEditType(12, QgsVectorLayer.ValueRelation)
vlayer.ValueRelationData( 'releatedtable20131027113855772', 'key', 'value',False, True)

I got the name of the related table by inspecting:


My guess is that I use the wrong string for referencing the layername. I have two questions: How to get the layername string to be used in ValueRelationData? Should I add further code to get it all working?


The correct approach is to directly modify the ValueRelationData object you get from vlayer.valueRelation( idx )

vlayer.setEditType(12, QgsVectorLayer.ValueRelation)
vlayer.valueRelation(12).mLayer = 'releatedtable20131027113855772'
vlayer.valueRelation(12).mKey = 'key'
vlayer.valueRelation(12).mValue = 'value'
vlayer.valueRelation(12).mAllowNull = False
vlayer.valueRelation(12).mOrderByValue = True
# vlayer.valueRelation(12).mAllowMulti = True
# vlayer.valueRelation(12).mFilterExpression = 'COALESCE( "fieldA", \'<NULL>\' )'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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