In C++ API if I am not mistaken there is class QgsDefaultValue, so we can programmatically set default values(expression) to a field.

Is there python alternative to that?

All research leads to defining trigger on create feature, but it is not really what I want.


There is a QgsDefaultValue class in QGIS but only starting with QGIS 3. It supports an additional flag, if the default value should also be applied on update. This exists in the C++ and the Python API.

For QGIS 2.18 there is a QgsVectorLayer::setDefaultValueExpression(fieldIndex, expression) function that does the same (without on update flag). This exists in the C++ and Python API.

  • that was the problem. still using 2.18 :) Thanks. – shika-kun Nov 21 '17 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.