Geographic Information Systems Asked on September 28, 2021
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 )
layer.attributeValueChanged.connect(update)
layer.geometryChanged.connect(update)
While updating fields for Updated Features, first step is to read the fields like below,
update_date=vlayer.fields().indexFromName('updated_date')
updated_user=vlayer.fields().indexFromName('updated_by')
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.
Answered by Vikram Aundhekar on September 28, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP