I want to populate a text field of a layer with a set of predefined attributes in QGIS 3.4.
For example, The field name is Type. The attributes set are AAA, BBB , CCC. I want to limit the entry of this field to the attribute sets so only one of these attribute can be entered in this field.
How can I limit the value of this field to the predefined attribute sets so that the user can enter them without typing?
1 Answer 1
You can do using the following steps:
- Go to
Layer Properties -> Attribute Form
- Select the target field
Type
in your case - Under
Widget Type
, selectValue Map
- Fill up the each row with the value you want
AAA
,BBB
, andCCC
. - Select
Unique
andEnforce Unique Constraint
Check the attribute table, you can only choose from the values you defined:
Update
Based on discussions with J.R, when you add a new feature, enabling Enforce Unique Constraint
will not let you add any of AAA, BBB, and CCC if they are already existing in the attribute table. You need to disable Enforce Unique Constraint
to get multiple same records.
-
the "Select Unique and Enforce Unique Constraint" will prevent to fill several identical value if you use the form to fill in attribute (if you fill tha attribute directly in the attribute table the constraint are not enforced and you can fill several time the same value...). In this case I don't think this setting is what we want as it seem that the value don't need to be unique, the setting to consider is the "Not null" constraint if you want to force user to fill a valueJ.R– J.R2019年10月25日 08:42:09 +00:00Commented Oct 25, 2019 at 8:42
-
@J.R Thanks for sharing your thoughts. In my understanding
Select Unique and Enforce Unique Constraint
is used to avoid duplicate values in the list itself but still can input many same values in the attribute table. UsingNot Null
will prevent null values but I am not if the OP wants to fill up all the rows insideType
column or want to keep some null rows. I leave this to the OP to decide.ahmadhanb– ahmadhanb2019年10月25日 08:52:16 +00:00Commented Oct 25, 2019 at 8:52 -
The unique constraint(or any other constraint) is for the attribute form only, try to create a few new point with the "show form on add feature" setting and you will see that you cant validate the form if you try to set several point with the same attribute. (As as said before if you open the attribute table to set the value the contraint is not enforced and you will be able to set the same value for all your point if you need)J.R– J.R2019年10月25日 09:04:04 +00:00Commented Oct 25, 2019 at 9:04
-
@J.R You are right, but if I uncheck
Enforce Unique Constraint
only, I found it accepts a same value to what is existing in the attribute table when I add a new feature (polygon in my case).ahmadhanb– ahmadhanb2019年10月25日 09:12:53 +00:00Commented Oct 25, 2019 at 9:12 -
1Yes, if you uncheck 'Enforce Unique Constraint' you just get warning that the uniqueness is not respected (the field turn orange and you get an orange cross right to it) but you can validate the form (so the constraint is here but QGIS is not enforcing it, it's up to you to respect the constraint or not). My point is to not setting constraint if you dont really need them regardless if you can bypass it or not.J.R– J.R2019年10月25日 09:25:38 +00:00Commented Oct 25, 2019 at 9:25