I have a vector layer, how to set defaults value in fields from the Python console.

Layer Properties / Attributes Form / Defaults | 

In option: Default value |______________|


You could make use of the QgsDefaultValue class which allows you to enter default values or expressions for specific fields based on their index:

layer = iface.activeLayer()
idx = layer.fields().indexFromName('ID')
layer.setDefaultValueDefinition(idx, QgsDefaultValue('3*4'))

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.