Skip to content

CRUDField

CRUDField

Bases: CRUDBase[FieldModel]

Create, Read, Update and Delete Field Models. Usually instantiated as part of a SchemaDefinition and accessed as .fields.

Base CRUD operations are common for both CRUDField and CRUDAction.

Example
import whyqd as qd

schema: qd.models.SchemaModel = {
    "name": "urban_population",
    "title": "Urban population",
    "description": "Urban population refers to people living in urban areas as defined by national statistical offices.",
}
fields: list[qd.models.FieldModel] = [
    {
        "name": "indicator_code",
        "title": "Indicator Code",
        "type": "string",
        "description": "World Bank code reference for Indicator Name.",
        "constraints": {"required": True},
    },
    {
        "name": "country_name",
        "title": "Country Name",
        "type": "string",
        "description": "Official country names.",
        "constraints": {"required": True},
    },
    {
        "name": "country_code",
        "title": "Country Code",
        "type": "string",
        "description": "UN ISO 3-letter country code.",
        "constraints": {"required": True},
    },
    {
        "name": "indicator_name",
        "title": "Indicator Name",
        "type": "string",
        "description": "Indicator described in the data series.",
        "constraints": {"required": True},
    },
    {
        "name": "year",
        "title": "Year",
        "type": "year",
        "description": "Year of release.",
        "constraints": {"required": True},
    },
    {
        "name": "values",
        "title": "Values",
        "type": "number",
        "description": "Value for the Year and Indicator Name.",
        "constraints": {"required": True},
    },
]
schema_destination = qd.SchemaDefinition()
schema_destination.set(schema=schema)
schema_destination.fields.add_multi(terms=fields)

Parameters:

Name Type Description Default
model Type[ModelType]

The Pydantic model on which CRUD operations are based.

required

get_category(*, name, category)

Get a specific field from the list of fields defining this schema, called by a unique name.

Parameters:

Name Type Description Default
name str

Field names must be unique, so a valid name in the field list will have no collisions.

required
category bool | str

Category name for a destination category term from the schema.

required

Raises:

Type Description
ValueError

If FieldModel has not category in ConstraintModel.

Returns:

Type Description
CategoryModel | None

A list of CategoryModel, or None of none are defined.

get_constraints(*, name)

Get the constraint parameters for a specific field defined in this schema, called by a unique name already in the schema.

Parameters:

Name Type Description Default
name str | UUID

Specific name or reference UUID for a field already in the Schema.

required

Returns:

Type Description
ConstraintsModel | None

A ConstraintsModel or None.

get_required()

Return a list of all required fields.

Returns:

Type Description
list[FieldModel]

A list of required fields.

set_categories(*, name, terms=None, as_bool=False, has_array=False)

Derive unique category constraints from a data column for a specific field, called by a unique name already in the schema.

Parameters:

Name Type Description Default
name str

Specific name for a field already in the Schema

required
terms DataFrame | Series | list[str] | None

Data provided as a table or column containing string terms, as arrays or individual terms

None
as_bool bool

Set the category terms as booleans, True and False

False
has_array bool

Set True if data terms are arrays, or the Field is of type FieldType.ARRAY

False

set_constraints(*, name, constraints)

Set the constraint parameters for a specific field to define this schema, called by a unique name already in the schema.

Parameters:

Name Type Description Default
name str | UUID

Specific name or reference UUID for a field already in the Schema

required
constraints ConstraintsModel | None

A dictionary conforming to the ConstraintsModel, or None. If None, then constraints are deleted.

required