3

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', '')
    layer.updateFeature(feature)

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

8

NULL is defined in qgis.core

from qgis.core import NULL

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

Your Answer

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

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