<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0);">Hi Hannes,</span></div>
<div class="elementToProof" style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0);"><br>
</span></div>
<div class="elementToProof" style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0);">i can unfortunately only confirm that this does not work, one can't seem to be able to set any field value (tried "text" as well). If you have the feature you can usually just do `feature["int"] = 12345` or feature.setAtrtribute("int",
 12345), but that does not have any effect on this particular feature - and it does not raise an error either.</span></div>
<div class="elementToProof" style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0);"><br>
</span></div>
<div class="elementToProof"><span style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);">I checked the feature's field map, also the value in "text", all seems right. One thing I noticed is that all newly created features in
 my test had the same feature.id() of </span><span style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);">-9223372036854775808</span><span style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);"> which
 seems rather high, usually, unsaved features have negative values, but counting downwards from 0 - at least from my last experience with PostGIS layers ... mayby memory layers behave different here.</span></div>
<div class="elementToProof"><span style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div class="elementToProof"><span style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);">I wonder if the feature you get from the event is the real feature that is added to the layer or some kind of read-only copy of it ...</span></div>
<div class="elementToProof"><span style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div class="elementToProof"><span style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);">Grüße</span></div>
<div class="elementToProof"><span style="font-family: "Century Gothic", sans-serif; font-size: 10pt; color: rgb(0, 0, 0);">Frank</span></div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>Von:</b> QGIS-Developer <qgis-developer-bounces@lists.osgeo.org> im Auftrag von Johannes Kröger (WhereGroup) via QGIS-Developer <qgis-developer@lists.osgeo.org><br>
<b>Gesendet:</b> Donnerstag, 4. Januar 2024 16:47<br>
<b>An:</b> qgis-developer <qgis-developer@lists.osgeo.org><br>
<b>Betreff:</b> [QGIS-Developer] Changing attributes of a newly created feature in Python form init code</font>
<div> </div>
</div>
<div>
<p>Hey guys,</p>
<p>I feel really dumb and I guess I am missing some basic concept...:</p>
<ul>
<li>I have a layer with two attributes: "text" (string) and "int" (integer).</li><li>I used drag'n'drop form design to have a form with just the "text" attribute in it.</li><li>I added Python Init Code:</li></ul>
<p>```<br>
from functools import partial<br>
<br>
def on_ok(layer, feature):<br>
    field_idx = layer.fields().indexOf("int")<br>
    attribute_changed = layer.changeAttributeValue(feature.id(), field_idx, 12345)<br>
    if not attribute_changed:<br>
        raise Exception("Attribute value could not be changed!")<br>
<br>
def my_form_open(dialog, layer, feature):<br>
    ok_button = dialog.findChild(QPushButton)<br>
    ok_button.clicked.connect(partial(on_ok, layer, feature))<br>
```</p>
<p>So the form does not show an input widget for the "int" field. But when pressing OK, the code should change the edited feature's "int" value to 12345.</p>
<p>This works if I edit existing features.</p>
<p>But if I create a new feature, the "int" value will be empty/NULL instead of 12345.</p>
Any pointers on how to properly update a new feature's attribute fields, that exist on a layer but are not shown the user with the QGIS' widgets in the attribute form?
<p>Cheers, Hannes</p>
</div>
</body>
</html>