I'm trying to set up a shapefile with a drop down for habitat type, that then autofills the corresponding code from a CSV file in the project. I've had a look at a few questions on here and thought I had it, but the dropdown menu for the code isn't populating.
this is the csv: csv attribute table
This is the shapefile attribute table: Shapefile attribute table
When I make a new shapefile, I can get the dropdown for the habitat, but it doesn't then autofill the "PrimCode" box.
This is the Value relation for habitat
and this is the Value relation for PrimCode Primcode value relation
I've tried both "PrimCode" and "Code" in the filter expression, and nothing seems to be working. I'm using QGIS, version 3.28.3.
-
See here: seems to be a similar (the same?) case as here: gis.stackexchange.com/a/489540/88814Babel– Babel2025年01月21日 15:15:37 +00:00Commented Jan 21 at 15:15
-
1Hi, sorry, I'm quite new to qGIS, but having looked at that one, it doesn't seem to be the same problem, but I might be wrong! I want a column to autofill based on what is selected in another column, so when someone selects a hab type, it autofills the hab code in the next column along, and pull that from a CSV that's in the project as well. I've just tried joining the CSV to the shapefile but it doesn't seem to have helped.Jdoolin– Jdoolin2025年01月22日 11:19:37 +00:00Commented Jan 22 at 11:19
1 Answer 1
Part 1: Select values from csv
To be able to choose from a dropdown menu in your vector layer in the field Habitat
and select the different textstrings defined in the csv layer, create a relation and set the widget accordingly:
In Project properties, Add a relation field
Habitat
from layer csv as parent, fieldHabitat
from layer vector as child.In peroperties of layer vector, for field
Habitat
, set Widget type toRelation Reference
and select fieldHabitat
as Display expression and make sure you have the correct relation selected. enter image description here
Part 2: Set a second field to autofill, based on first field
For Feild PrimCode, set Widget type to Text Edit and define a default value, based on what you entered in field
Habitat
and look up in the csv table which code is associated with this value. Use this expression:attribute(get_feature('csv', 'Habitat', "Habitat"), 'Code')
.Make sure to have checked the box Apply default value on update and uncheck the box Editable (as you want to avoid editing this field manually, it should just display the defined value).
When you now select a value in the dropdown menu in the field Habitat
, the according code is automaticalle introduced in field PrimCode
: