Skip to main content
12 events
when toggle format what by license comment
Jan 24, 2019 at 13:18 comment added Ben W @lhowarth, after I posted my answer above, I read your comments about your problems updating your new column with a value calculated from joined fields. I will be interested to see if my code above works or whether you have the same problem.
Jan 24, 2019 at 12:46 comment added lhowarth This is odd, if I create a new field and use the field calculater in the attribute table i don't have this problem with joined fields...
Jan 24, 2019 at 12:41 comment added lhowarth ok, so your script works if the fields are native to the table (i.e. not produced through a join)....
Jan 24, 2019 at 12:33 comment added lhowarth hmmm, just wondering my Field 1 and Field 2 are actually produced by a join... i'll see if i have same problem if I use other fields which are "native" to the table
Jan 24, 2019 at 12:31 comment added lhowarth Hi, I'm just using your script (but with field names modified) and yes layers was activated. 'layer = iface.activeLayer() fieldlist = ['field 1','field 2','Test'] field_indexes = [layer.dataProvider().fieldNameIndex(f) for f in fieldlist] layer.startEditing() for feature in layer.getFeatures(): _=layer.changeAttributeValue(feature.id(),field_indexes[-1], sum(feature.attributes()[field_indexes[0]:field_indexes[1]+1])) layer.commitChanges()' After the script runs it replaces the null values in the new field by 0 - so it finds the table and the new field...
Jan 24, 2019 at 12:22 comment added Bera I just tried it on all int fields and it worked. Have you activated the layer by clicking it in table of contents? Are you only using my code or have you mixed it with your own?
Jan 24, 2019 at 12:16 comment added lhowarth it doesn't seem to change the result
Jan 24, 2019 at 11:49 comment added lhowarth @ BERA - they're all integers so i don't think it'll be that, but I'll try and make my Sum1_2 field an Integer64 and see if that changes the result
Jan 24, 2019 at 11:45 comment added lhowarth @ BERA Field 1 and Field 2 are both integer64 my Sum1_2 field is straight Integer
Jan 24, 2019 at 11:23 comment added Bera @lhowarth Not sure why you get 0. I tried it on two different shapefiles with success. What is the field type of your fields?
Jan 24, 2019 at 10:59 comment added lhowarth Thanks, I thought fieldNameIndex was replaced in QGIS 3 ? That said, it I have tried your script and it does seem to work :) - partly - I nolonger have NULL values in the field but everything equals 0 - which is wrong - so I'm guessing it's to do with the field_indexes ?
Jan 24, 2019 at 10:33 history answered Bera CC BY-SA 4.0