I have a layer with one field and I would like to rename that field with PyQGIS.
I guess it can be done using the QGIS tool "refactorfields" but I don't know how to use it in PyQGIS...
My other idea was to create a new field and next copy the values from field1 to the new field and than delete field1.
That is the code I came up with the help of the docs:
layer.dataProvider().addAttributes([QgsField("Field_xy", QVariant.String)]) for feature in layer.getFeatures(): id = feature.attributes() feature.setAttribute('Field_xy', id) channel2.updateFeature(feature) layer.dataProvider().deleteAttributes()
unfortunately, I get the following error message:
It refers to the
feature.setAttribute() line. I guess it means that the field 'Field_xy' doesn't exist. But I've created the field in the line above the for loop haven't I?