Class: QgsField

class qgis.core.QgsField

Bases: sip.wrapper

Encapsulate a field in an attribute table or data source.

QgsField stores metadata about an attribute field, including name, type length, and if applicable, precision.

Note

QgsField objects are implicitly shared.

QgsField(name: str = ‘’, type: QVariant.Type = QVariant.Invalid, typeName: str = ‘’, len: int = 0, prec: int = 0, comment: str = ‘’, subType: QVariant.Type = QVariant.Invalid) Constructor. Constructs a new QgsField object.

Parameters:
  • name – Field name

  • type – Field variant type, currently supported: String / Int / Double

  • typeName – Field type (e.g., char, varchar, text, int, serial, double). Field types are usually unique to the source and are stored exactly as returned from the data store.

  • len – Field length

  • prec – Field precision. Usually decimal places but may also be used in conjunction with other fields types (e.g., variable character fields)

  • comment – Comment for the field

  • subType – If the field is a collection, its element’s type. When all the elements don’t need to have the same type, leave this to QVariant.Invalid.

QgsField(other: QgsField) Copy constructor

Methods

alias

Returns the alias for the field (the friendly displayed name of the field ), or an empty string if there is no alias.

comment

Returns the field comment

constraints

Returns constraints which are present for the field.

convertCompatible

Converts the provided variant to a compatible format

defaultValueDefinition

Returns the expression used when calculating the default value for the field.

displayName

Returns the name to use when displaying this field.

displayNameWithAlias

Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined.

displayString

Formats string for display

displayType

Returns the type to use when displaying this field, including the length and precision of the datatype if applicable.

editorWidgetSetup

Gets the editor widget setup for the field.

friendlyTypeString

Returns a user friendly, translated representation of the field type.

isDateOrTime

Returns if this field is a date and/or time type.

isNumeric

Returns if this field is numeric.

isReadOnly

Returns True if this field is a read-only field.

length

Gets the length of the field.

metadata

Returns the map of field metadata.

name

Returns the name of the field.

precision

Gets the precision of the field.

setAlias

Sets the alias for the field (the friendly displayed name of the field ).

setComment

Set the field comment

setConstraints

Sets constraints which are present for the field.

setDefaultValueDefinition

Sets an expression to use when calculating the default value for the field.

setEditorWidgetSetup

Set the editor widget setup for the field.

setLength

Set the field length.

setMetadata

Sets the map of field metadata.

setName

Set the field name.

setPrecision

Set the field precision.

setReadOnly

Make field read-only if readOnly is set to true.

setSplitPolicy

Sets the field's split policy, which indicates how field values should be handled during a split operation.

setSubType

If the field is a collection, set its element's type.

setType

Set variant type.

setTypeName

Set the field type.

splitPolicy

Returns the field's split policy, which indicates how field values should be handled during a split operation.

subType

If the field is a collection, gets its element's type.

type

Gets variant type of the field as it will be retrieved from data source

typeName

Gets the field type.

Attributes

staticMetaObject

alias(self) str

Returns the alias for the field (the friendly displayed name of the field ), or an empty string if there is no alias.

See also

setAlias()

New in version 3.0.

Return type:

str

comment(self) str

Returns the field comment

Return type:

str

constraints(self) QgsFieldConstraints

Returns constraints which are present for the field.

See also

setConstraints()

New in version 3.0.

Return type:

QgsFieldConstraints

convertCompatible(self, v: Any) bool

Converts the provided variant to a compatible format

Parameters:

v (Any) – The value to convert

Raises:

ValueError – if the value could not be converted to a compatible format

Return type:

bool

defaultValueDefinition(self) QgsDefaultValue

Returns the expression used when calculating the default value for the field.

Return type:

QgsDefaultValue

Returns:

expression evaluated when calculating default values for field, or an empty string if no default is set

New in version 3.0.

displayName(self) str

Returns the name to use when displaying this field. This will be the field alias if set, otherwise the field name.

See also

name()

See also

alias()

New in version 3.0.

Return type:

str

displayNameWithAlias(self) str

Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined.

This will be used when working close to the data structure (i.e. building expressions and queries), when the real field name must be shown but the alias is also useful to understand what the field represents.

See also

name()

See also

alias()

New in version 3.12.

Return type:

str

displayString(self, v: Any) str

Formats string for display

Parameters:

v (Any) –

Return type:

str

displayType(self, showConstraints: bool = False) str

Returns the type to use when displaying this field, including the length and precision of the datatype if applicable.

This will be used when the full datatype with details has to displayed to the user.

See also

type()

New in version 3.14.

Parameters:

showConstraints (bool = False) –

Return type:

str

editorWidgetSetup(self) QgsEditorWidgetSetup

Gets the editor widget setup for the field.

Defaults may be set by the provider and can be overridden by manual field configuration.

Return type:

QgsEditorWidgetSetup

Returns:

the value

friendlyTypeString(self) str

Returns a user friendly, translated representation of the field type.

Unlike displayType(), this method only returns strings representing the field type and does not include length, precision or constraint information.

See also

type()

See also

displayType()

New in version 3.14.

Return type:

str

isDateOrTime(self) bool

Returns if this field is a date and/or time type.

New in version 3.6.

Return type:

bool

isNumeric(self) bool

Returns if this field is numeric. Any integer or floating point type will return True for this.

New in version 2.18.

Return type:

bool

isReadOnly(self) bool

Returns True if this field is a read-only field. This is the case for providers which support generated fields for instance.

New in version 3.18.

Return type:

bool

length(self) int

Gets the length of the field.

Return type:

int

Returns:

int containing the length of the field

metadata(self) Dict[int, Any]

Returns the map of field metadata.

Map keys should match values from the Qgis.FieldMetadataProperty enum.

See also

setMetadata()

New in version 3.32.

metadata(self, property: int) -> Any Returns a specific metadata property.

See also

setMetadata()

New in version 3.32.

Return type:

Dict[int, Any]

name(self) str

Returns the name of the field.

See also

setName()

See also

displayName()

Return type:

str

precision(self) int

Gets the precision of the field. Not all field types have a related precision.

Return type:

int

Returns:

int containing the precision or zero if not applicable to the field type.

setAlias(self, alias: str)

Sets the alias for the field (the friendly displayed name of the field ).

Parameters:

alias (str) – field alias, or empty string to remove an existing alias

See also

alias()

New in version 3.0.

setComment(self, comment: str)

Set the field comment

Parameters:

comment (str) –

setConstraints(self, constraints: QgsFieldConstraints)

Sets constraints which are present for the field.

See also

constraints()

New in version 3.0.

Parameters:

constraints (QgsFieldConstraints) –

setDefaultValueDefinition(self, defaultValueDefinition: QgsDefaultValue)

Sets an expression to use when calculating the default value for the field.

Parameters:

defaultValueDefinition (QgsDefaultValue) – expression to evaluate when calculating default values for field. Pass a default constructed :py:class:`QgsDefaultValue`() to reset.

New in version 3.0.

setEditorWidgetSetup(self, v: QgsEditorWidgetSetup)

Set the editor widget setup for the field.

Parameters:

v (QgsEditorWidgetSetup) – The value to set

setLength(self, len: int)

Set the field length.

Parameters:

len (int) – Length of the field

setMetadata(self, metadata: Dict[int, Any])

Sets the map of field metadata.

Map keys should match values from the Qgis.FieldMetadataProperty enum.

See also

metadata()

New in version 3.32.

setMetadata(self, property: int, value: Any) Sets a metadata property to value.

See also

metadata()

New in version 3.32.

Parameters:

metadata (Dict[int) –

setName(self, name: str)

Set the field name.

Parameters:

name (str) – Name of the field

setPrecision(self, precision: int)

Set the field precision.

Parameters:

precision (int) – Precision of the field

setReadOnly(self, readOnly: bool)

Make field read-only if readOnly is set to true. This is the case for providers which support generated fields for instance.

New in version 3.18.

Parameters:

readOnly (bool) –

setSplitPolicy(self, policy: Qgis.FieldDomainSplitPolicy)

Sets the field’s split policy, which indicates how field values should be handled during a split operation.

See also

splitPolicy()

New in version 3.30.

Parameters:

policy (Qgis.FieldDomainSplitPolicy) –

setSubType(self, subType: QVariant.Type)

If the field is a collection, set its element’s type. When all the elements don’t need to have the same type, set this to QVariant.Invalid.

New in version 3.0.

Parameters:

subType (QVariant.Type) –

setType(self, type: QVariant.Type)

Set variant type.

Parameters:

type (QVariant.Type) –

setTypeName(self, typeName: str)

Set the field type.

Parameters:

typeName (str) – Field type

splitPolicy(self) Qgis.FieldDomainSplitPolicy

Returns the field’s split policy, which indicates how field values should be handled during a split operation.

See also

setSplitPolicy()

New in version 3.30.

Return type:

Qgis.FieldDomainSplitPolicy

staticMetaObject = <PyQt5.QtCore.QMetaObject object>
subType(self) QVariant.Type

If the field is a collection, gets its element’s type. When all the elements don’t need to have the same type, this returns QVariant.Invalid.

New in version 3.0.

Return type:

QVariant.Type

type(self) QVariant.Type

Gets variant type of the field as it will be retrieved from data source

Return type:

QVariant.Type

typeName(self) str

Gets the field type. Field types vary depending on the data source. Examples are char, int, double, blob, geometry, etc. The type is stored exactly as the data store reports it, with no attempt to standardize the value.

Return type:

str

Returns:

QString containing the field type