I would like to implement a function to check that there are no features already inserted with the same value in a certain field.
This is the function that I implemented as Action but during editing [%id_lavoro%] always seems to be empty
from qgis.utils import iface
from qgis.PyQt.QtWidgets import *
msg = QMessageBox()
l = iface.activeLayer()
expr = QgsExpression( "\"id_lavoro\"=[%id_lavoro%]")
it = l.getFeatures(QgsFeatureRequest(expr))
ids = str([i.id() for i in it])
msg.setText(ids)
msg.show()
QGIS has a function (current_value) but I don't know how to use it in Python code
-
I continue to do research but it seems there is no way to return value of a field in an unsaved form.......Giovanni– Giovanni2023年11月17日 06:19:55 +00:00Commented Nov 17, 2023 at 6:19
1 Answer 1
From what I understand, you don't need any action here. You can just set a unique constraint on your field.
See: https://www.youtube.com/watch?v=gG0Yg5JRDP8 for how to setup this on your form.
-
My control (Action) is only for warning, it must not be a constraintGiovanni– Giovanni2023年11月18日 16:04:57 +00:00Commented Nov 18, 2023 at 16:04
-
If you only check the "unique" checkbox, it won't act as a constraint but as a warning. Maybe you need to don't check "enforce unique constraint"?.YoLecomte– YoLecomte2023年11月20日 08:16:10 +00:00Commented Nov 20, 2023 at 8:16
-
What I would like is to show a list of the other rows (list of rows IDs) that refer to the same "id_lavoro" so the user can check if it is an error or notGiovanni– Giovanni2023年11月21日 19:07:04 +00:00Commented Nov 21, 2023 at 19:07