Friday, 3 July 2015

pyqgis - Making automatic updated date and time field in QGIS


I wish to create a field which automatically updates with the current timestamp when edits are made to a feature.


I am using QGIS 3.4 and I am testing this using a shapefile.



Following @Joseph's answer for this question I tried updating his script to work in QGIS 3.4 as per his instructions (if I followed them correctly, not guaranteed!) to this:


layer = qgis.utils.iface.activeLayer()

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


layer.attributeValueChanged.connect(update)

However this returns the following error:


2018-11-29T11:13:00     WARNING    Traceback (most recent call last):
File "", line 4, in update
AttributeError: 'QgsVectorLayer' object has no attribute 'pendingFields'

What's wrong here?


Also will this work on geometry edits or only with attribute edits?



Will this also work for a SpatiaLite layer?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...