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