1

I have a group of layers and I want to edit the field in some of them. More specifically, I want to change the length in those fields named ATTRIBUTE1. I'm using this code

from qgis import core
from qgis.core import QgsApplication
from qgis.core.additions.edit import edit
from qgis.core import QgsProject


# Get the project instance
project = QgsProject.instance()

# load layer tree
root = project.instance().layerTreeRoot()

# extract group
wip_layer = root.findGroup("Layers").findGroup("wip_equipaments_polygons")

# get all layers in the group
layers = [c.layer() for c in wip_layer.children()]

field_of_interest = "ATTRIBUTE1"



for layer in layers:
    layer: core.QgsVectorLayer
    with edit(layer):
        for field in layer.fields():
            field: core.QgsField
            if field.typeName() == "String":
                if field.name() == field_of_interest:
                    print("field found")
                    print("before", field.name(), field.length(), end="->")
                    field.setLength(50)
                    print("after", field.name(), field.length())
                    layer.updateFields()

I'm getting this result

field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 50->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50
field found
before ATTRIBUTE1 8->after ATTRIBUTE1 50

But changes in the layers are not being persisted. What am I doing wrong?

Related:

0

Your Answer

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

Browse other questions tagged or ask your own question.