How-to: Define Object Types and Properties¶
This how-to guide shows how to create new Object Types in bam-masterdata, including how to define the
Object Type itself and the properties assigned to it. It also covers the optional units field
for property definitions.
Create a New Object Type¶
Object types live in bam_masterdata/datamodel/object_types.py and are defined as subclasses of
ObjectType or of any other Object Type. Each object type must include:
defs: anObjectTypeDefinstance describing the object type static metainformation- one or more
PropertyTypeAssignmententries describing assigned properties
Example:
from bam_masterdata.metadata.definitions import DataType, ObjectTypeDef, PropertyTypeAssignment
from bam_masterdata.metadata.entities import ObjectType
class TestSpecimen(ObjectType):
defs = ObjectTypeDef(
code="TEST_SPECIMEN",
description="Test specimen used in experiments//Testkoerper fuer Versuche",
generated_code_prefix="TSP",
auto_generate_codes=True,
)
name = PropertyTypeAssignment(
code="NAME",
data_type=DataType.VARCHAR,
property_label="Name",
description="Human readable name//Name",
mandatory=True,
show_in_edit_views=True,
section="General",
)
ObjectTypeDef Options¶
ObjectTypeDef defines static metadata for the object type:
code: OpenBIS object type code (uppercase with underscores)description: English text with optional German text separated by//generated_code_prefix(optional): short prefix used for auto-generated codesauto_generate_codes(optional): setTrueto auto-generate codes using the prefix
Rules:
codemust be uppercase, use underscores, and may include inheritance with dots.descriptionshould be human-readable and complete enough; use//to add the German version.- If
generated_code_prefixis not provided, it defaults to the first three letters ofcode.
PropertyTypeAssignment Options¶
Each PropertyTypeAssignment supports:
code: OpenBIS property code (uppercase with underscores)data_type: one of theDataTypeenum values (see Data Types for all the options)property_label: human-readable label shown in the GUIdescription: English and optional German text (separated by//)mandatory: ifTrue, the property must be setshow_in_edit_views: ifTrue, show the property in edit viewssection: section label used in the GUI to group propertiesvocabulary_code(optional): required forCONTROLLEDVOCABULARYobject_code(optional): required forOBJECTunits(optional): units in pint format
Other options are being deprecated in a near future, that is why they are not included here.
Rules:
codemust be uppercase, use underscores, and may include inheritance with dots.descriptionuses//to include German text.- If
data_typeisCONTROLLEDVOCABULARY, you must setvocabulary_code. - If
data_typeisOBJECT, you must setobject_code. property_labelmust be human-readable and should not include internal codes.- When
unitsis set,property_labelwill automatic includein [units]when displayed on the GUI.
Data Types¶
data_type must be one of the DataType enum values. Common choices:
VARCHAR,MULTILINE_VARCHARfor textINTEGER,REALfor numeric valuesDATE,TIMESTAMPfor date/timeBOOLEANfor true/falseHYPERLINKfor URLsCONTROLLEDVOCABULARYfor pick-lists (requiresvocabulary_code)OBJECTfor references to other object types (requiresobject_code)
For object references, see How-to: Work with Object References.
Units with pint¶
You can optionally attach units to any property using units. Units are parsed using pint and must
be valid according to the default pint registry.
When units is provided, property_label is automatically suffixed with in [units] unless it
already includes in [units]. If it contains brackets but not the in [units] format, validation
fails.
Example:
temperature = PropertyTypeAssignment(
code="TEMPERATURE",
data_type=DataType.REAL,
property_label="Temperature",
description="Measurement temperature//Messtemperatur",
mandatory=False,
show_in_edit_views=True,
section="Measurements",
units="degC",
)
# property_label becomes: "Temperature in [degC]"
If your property_label already includes in [units], it will not be modified: