In my standalone QGIS script I'm selecting a couple of polygons and want to set its value in the field 'distance' to NULL.

I checked the PyQGIS Documentation, past SE Questions and came up with the following code:

for feature in layer.selectedFeatures():
    feature.setAttribute('distance', '')

return layer

The selection works perfectly fine but it doesn't change the attributes. According to the docs the function setAttribute() takes the field name as first variable and than the input you like it to have.

I also tried to use None instead of ' ', but it didn't work either.

What am I doing wrong?

I'm using QGIS 3.10

1 Answer 1


NULL is defined in qgis.core

from qgis.core import NULL

with edit(layer):
    for feature in layer.selectedFeatures():
        feature.setAttribute('distance', NULL)
  • 2
    The layer needs to be in edit mode. E.g. through with edit(layer). Commented May 9, 2020 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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