QGIS PyQGIS – Resolve AttributeError on ‘QgsVectorLayer’ PendingFields

attributeerrorpyqgisqgisvector

I have been using the following Python code on QGIS 3 (2.99) to look up fields, for several months

id = feature.attributes()[layer.pendingFields().lookupField('ID')]

And it gives me an error today:

AttributeError: 'QgsVectorLayer' object has no attribute 'pendingFields'

Is this another breaking change in the QGIS development version? And what's the correct way to write it now?

Best Answer

The answer seems to be yes, another change in QGIS 2.99.

I saw a question (not available) from @Andreas Neumann on the qgis-dev mailing list regarding the same question.

@Etienne Trimaille and @Alessandro Pasotti reveal that pendingFields() is removed and suggest that you instead should use fields().