0

layer.renameAttribute(0,'xxx') Traceback (most recent call last): File "", line 1, in AttributeError: 'QgsVectorLayer' object has no attribute 'renameAttribute'

Can you get me solution for this error?

3
  • 2
    The QgsVectorLayer::renameAttribute() method was introduced in QGIS 2.16. I'm guessing you're using an earlier version?
    – Joseph
    Commented Jan 25, 2018 at 10:24
  • yes i'm using 2.14.3 Commented Jan 25, 2018 at 11:05
  • Could you update to a more recent version (e.g. 2.18)? Otherwise you could try the method posted here.
    – Joseph
    Commented Jan 25, 2018 at 11:08

1 Answer 1

2

I had the same issue. Try this piece of code. It worked for me.

layer = iface.activeLayer()
for field in layer.pendingFields():
    if field.name() == 'old':
        with edit(layer):
            idx = layer.fieldNameIndex(field.name())
            layer.renameAttribute(idx, 'new')
2
  • no tried this too,but same error is generating every time,do i need to import ant file for this to run? Commented Jan 25, 2018 at 10:44
  • This code is based on QGIS 3.0 not on 2.0 which causes the error in QGIS < 3.0 Commented Nov 12, 2019 at 12:35

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