Like ArcGIS Editor Tracking, I am trying to design attribute field with self update date feature called Updated Date in QGIS. I am following Making automatic updated date and time field in QGIS this link to get calculate it automatically but while doing so, the QGIS project is getting crashed every time. I am selecting only one feature but still it is getting crashed. I checked the code and it is running fine in Python console but not in Plugin. Please suggest.

Python Code for reference:

layer = qgis.utils.iface.activeLayer()

def update():
    field = layer.fields().lookupField('fieldName')
    e = QgsExpression( " $now " )
    for feat in layer.selectedFeatures():
        feat[field] = e.evaluate()
        layer.updateFeature( feat )

  • 1
    Have you tried the "Default value" functionality that comes out of the box with QGIS? user-images.githubusercontent.com/652785/… You can even set it programmatically. Commented Jul 15, 2020 at 13:18
  • @GermánCarrillo, I tried the way you mentioned with Python and it worked, Thank you! Commented Jul 16, 2020 at 6:45
  • Great! You can post the way you finally solved it, and even accept the answer, so that future visitors can also know how to solve it. Commented Jul 21, 2020 at 2:48

1 Answer 1


While updating fields for Updated Features, first step is to read the fields like below,


After this, we can use the existing property of QGIS (QgsDefaultValue) in Python like written below,

vlayer.setDefaultValueDefinition(update_date, QgsDefaultValue('$now',True))
vlayer.setDefaultValueDefinition(updated_user, QgsDefaultValue("'{}'".format(str(uri.username())),True))

In the above expression it is necessary to mention True, to get to know about field update only on feature update. This is how QGIS stopped getting crashed.

Some other approaches are really appreciated.

  • That's the best approach. Single API method and you're done. Commented Aug 30, 2020 at 13:11

Your Answer

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

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