Get only fields with datatype int in PyQGIS
By type name (typeName()
):
int_types = ["Integer", "Integer64"]
int_fields = [field for field in iface.activeLayer().fields() if field.typeName() in int_types]
OR by type number (type()
):
int_types = [2, 4] # 2: Integer, 4: Integer64
int_fields = [field for field in iface.activeLayer().fields() if field.type() in int_types]
Type of some fields may be Integer64
. Integer64
equals QVariant.LongLong
actually, but it could be considered as integer.
layer = iface.activeLayer()
intfields = [f for f in layer.fields() if f.typeName().startswith('Int')]
print(intfields)
Found typeName here