PyQGIS – Resolving AttributeError for QgsVectorLayer Object FieldNameIndex

pyqgisqgis-3

When I try to calculate a new field. I receive the following error:

idx = self.layer.fieldNameIndex(attributeName)
AttributeError: 'QgsVectorLayer' object has no attribute 'fieldNameIndex'

What´s the problem? I'm using QGIS 3.0.1 64x in windows.

Best Answer

in python 3: use

fields().lookupField() 

or fields().indexFromName()

example:

idx= layer.fields().indexFromName(attributeName)