I exported a model from model builder to python. But qgis:refactorfields does not accept @row_number as expression.

outputs_QGISREFACTORFIELDS_1=processing.runalg('qgis:refactorfields', outputs_QGISMULTIPARTTOSINGLEPARTS_1['OUTPUT'],'[{'type': 2, 'length': 3, 'expression': u'@row_number', 'name': u'cluster', 'precision': 0}]',cluster)
      SyntaxError: invalid syntax
1   Cannot find variable: row_number
2   Uncaught error while executing algorithm
Traceback (most recent call last):
          File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\core\GeoAlgorithm.py", line 203, in execute
          File "C:/OSGEO4~1/apps/qgis/./python/plugins\processing\script\ScriptAlgorithm.py", line 378, in processAlgorithm
            exec((script), ns)
          File "<string>", line 9

I tried with $row_num, it's not working.


I adapted your script to my system and it worked when I changed this part of code:

..., '[{'type': 2, 'length': 3, 'expression': u'@row_number', 'name': u'cluster', 'precision': 0}]', ...

in this one:

..., [{'type': 2, 'length': 3, 'expression': u'@row_number', 'name': u'cluster', 'precision': 0}], ...

My complete code was:

import processing

route = iface.activeLayer()

path = outputs_QGISREFACTORFIELDS_1=processing.runalg('qgis:refactorfields', 
                                                      [{'type': 2, 
                                                      'length': 3, 
                                                      'expression': u'@row_number', 
                                                      'name': u'cluster', 
                                                      'precision': 0}],

output = QgsVectorLayer(path['OUTPUT_LAYER'],


and produced a result when it was ran at the Python Console of QGIS:

enter image description here

  • Thks, so it s considered as a bug in the ecport to python feature? – jlSta Feb 24 '17 at 8:35
  • it's the same problem with qgis:fieldcalculator , true and other fields should be between " " – jlSta Feb 24 '17 at 8:42
  • You're welcome. In relation to your question I don't know. I just tried out your code to find out where the issue was. – xunilk Feb 24 '17 at 8:42
  • You can see an example of use here: docs.qgis.org/2.8/en/docs/user_manual/processing_algs/qgis/… and for the fieldcalculator here: docs.qgis.org/2.8/en/docs/user_manual/processing_algs/qgis/… – xunilk Feb 24 '17 at 8:46
  • Yes thanks for your answer. I mean a bug about the export module in the model builder. It doesn't export the fields correctly it adds '' or wipe out the " " – jlSta Feb 24 '17 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.