Hi, You should use the method in QgsVectorLayer, otherwise, you are updating a "copy" of the QgsField :
for field in layer.fields(): layer.setFieldConfigurationFlag( layer.fields().indexFromName(field.name()), Qgis.FieldConfigurationFlag.HideFromWfs, True) On GitHub.com, I often search for a piece of code to see existing plugin how they deal with a piece of code : https://github.com/search?q=FieldConfigurationFlag+language%3APython&type=code&l=Python There is only one script, as this API is quite new, but still nice to know for next time. Le jeu. 1 août 2024 à 17:09, Roland Berger via QGIS-User < qgis-user@lists.osgeo.org> a écrit : > Hi > > I try to set the configuration for a vector layer with python. > Below is one of the many ways I tried to do it. > Now I'm of out of ideas (chatgpt, intellij ai assistent and google as > well) and would be thankful for a working example. > > Thanks Roland > > Code you can use in a Python Console Editor. First it creates a layer to > use for the test and then tries to set the field configuration. > > # ================= > # Setup Layer with field > # ================= > > # Create a vector layer > layer = QgsVectorLayer("Point?crs=EPSG:4326", "MyLayer", "memory") > if not layer.isValid(): > print("Failed to create the layer!") > sys.exit(1) > > # Add fields to the layer > layer_data_provider = layer.dataProvider() > layer_data_provider.addAttributes([QgsField("field1", QVariant.String)]) > layer.updateFields() > > # Add a feature to the layer > feature = QgsFeature(layer.fields()) > feature.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(0, 0))) > feature.setAttribute("field1", "value1") > layer_data_provider.addFeature(feature) > layer.updateExtents() > > # Add the layer to the project > project = QgsProject.instance() > project.addMapLayer(layer) > > # ================= > # Try to set field configuration > # ================= > layer.startEditing() > for field in layer.fields(): > config_flags = field.configurationFlags() > config_flags |= Qgis.FieldConfigurationFlag.HideFromWfs > field.setConfigurationFlags(config_flags) > #layer.updateFields() > layer.commitChanges() > > > > > _______________________________________________ > QGIS-User mailing list > QGIS-User@lists.osgeo.org > List info: https://lists.osgeo.org/mailman/listinfo/qgis-user > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user >
_______________________________________________ QGIS-User mailing list QGIS-User@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-user Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user