2

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

enter image description here

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 14, 2023 at 14:32
1
  • I continue to do research but it seems there is no way to return value of a field in an unsaved form....... Commented Nov 17, 2023 at 6:19

1 Answer 1

0

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.

answered Nov 17, 2023 at 13:39
3
  • My control (Action) is only for warning, it must not be a constraint Commented 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"?. Commented 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 not Commented Nov 21, 2023 at 19:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.