2

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

7

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)
| improve this answer | |
  • 2
    The layer needs to be in edit mode. E.g. through with edit(layer). – Matthias Kuhn May 9 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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