[GIS] Limiting available values in dropdown field of QGIS form according to value selected in previous field

expressionfield-mappingpostgisqgisvalue-list

I'm building a rather complex entry form for a layer stored in a PostGIS database. I am following this usage scenario:

On several occasions I have dropdown fields which lets the user select a type of artefact (populated through value relation to another table in a database), followed immediately with the field letting them select a subtype (also populated through value relation to another table in a database). I would like a choice of subtypes in the dropdown to be limited by the previous selection of a type.

For example, if type "ceramics" is selected the subtype dropdown needs to be limited to only options with a key value from 1 to 10.

If type "metals" is selected the subtype dropdown needs to be limited to only options with a key value from 11 to 20.

And so on.

I feel relatively fluent in SQL and qgis expression builder, and i would guess this could be achieved trough filter expression option on value relation widget, but I wouldn't know how to invoke the values from a form which is currently being filled.

I suspect some python code is needed? – I'm not fluent in python but I guess I could adapt an example of code to my needs by renaming the fields etc.

Question is similar to Field Value Mapping QGIS but the answers there didnt point me out in directions I could apply with my current knowledge.

Best Answer

If you create a table which as all the values you need and add to your map. Create a relationship (Project>properties>relations). Then when building your form in the edit widget properties, select value relation Here you can filter what is displayed (ie if x then show values where filterField = y)

Related Question