I am trying to set a field constraint for a specific field called 'node_id' for node layer. Below is the code:
layer = iface.activeLayer()
field_index = layer.fields().indexFromName("node_id")
constraint_expression = '"node_id"==0 or "node_id" == 1'
layer.setFieldConstraint(field_index, QgsFieldConstraints.Constraint(constraint_expression))
layer.updateFields()
I am facing this error:
ValueError: invalid literal for int() with base 10: '"node_id" not NULL'
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Aug 2, 2023 at 10:37
1 Answer 1
layer.setFieldConstraint(field_index, QgsFieldConstraints.ConstraintExpression)
layer.setConstraintExpression(field_index, '"node_id" in (0, 1)')
answered Aug 2, 2023 at 11:00
-
I want to set an expression like - 'node_id' ==1 or 0 no other value. So thats why I need expression @RainForestBenStone– BenStone2023年08月02日 11:04:45 +00:00Commented Aug 2, 2023 at 11:04
-
I have edited the expression, need to set the expression for the specific field - Node id should have value 0 or 1.BenStone– BenStone2023年08月02日 11:09:53 +00:00Commented Aug 2, 2023 at 11:09
-
Thank you , this worksBenStone– BenStone2023年08月02日 11:32:08 +00:00Commented Aug 2, 2023 at 11:32
-
One small question: Is there any way to add a tooltip/placeholder for a field using pyqgis?BenStone– BenStone2023年08月04日 09:15:03 +00:00Commented Aug 4, 2023 at 9:15
-
layer.setFieldAlias(field_index, 'This Field Alias')RainForest– RainForest2023年08月04日 10:32:47 +00:00Commented Aug 4, 2023 at 10:32
lang-py