fairdm.registry.registry#
The registry of Sample and Measurement models, and the register decorator.
Module Contents#
Classes#
Registry of Sample and Measurement models and their configurations. |
Functions#
Register a Sample or Measurement model with its configuration. |
Data#
API#
- class FairDMRegistry[source]#
Registry of Sample and Measurement models and their configurations.
A model is registered with a configuration class that auto-generates forms, serializers, filters and tables when they are not given explicitly.
Usage: @fairdm.register class MySampleConfig: model = MySample display_name = “Water Sample” list_fields = [“name”, “location”, “collected_at”] detail_fields = [“name”, “description”, “metadata”] filter_fields = [“collected_at”, “contributor”]
Initialization
- get_for_model(model_reference: type[django.db.models.Model] | str) fairdm.registry.config.ModelConfiguration[source]#
Return the registered configuration for a model.
Args: model_reference: A Django model class, or a string in the format
"app_label.model_name"as accepted byapps.get_model. Use the Django app label and the lowercase model name, for example"sample.sample"for the core Sample model.Returns: The configuration instance for the model.
Raises: NotRegisteredError: The model is not registered. It is also a
KeyError. ValueError: The string is not in the format"app_label.model_name". LookupError: The model cannot be found in the Django apps.Example: config = registry.get_for_model(MySample) config = registry.get_for_model(“myapp.mysample”)
- is_registered(model_reference: type[django.db.models.Model] | str) bool[source]#
Report whether a model is registered.
Args: model_reference: A Django model class, or a string in the format
"app_label.model_name".Returns:
Trueif the model is registered,Falseotherwise, including when the reference does not resolve to a model.Example: if registry.is_registered(“myapp.mysample”): …
- property samples: list[type[fairdm.core.sample.models.Sample]]#
Return the registered Sample model classes.
- property measurements: list[type[fairdm.core.measurement.models.Measurement]]#
Return the registered Measurement model classes.
- property models: list[type[django.db.models.Model]]#
Return every registered model class, Samples and Measurements together.
- get_all_configs() list[fairdm.registry.config.ModelConfiguration][source]#
Return every registered configuration.
Returns: The
ModelConfigurationinstances in registration order.Example: for config in registry.get_all_configs(): print(config.model.name, config.fields)
- register(model_class: type[django.db.models.Model], config: fairdm.registry.config.ModelConfiguration | None = None) None[source]#
Register a Sample or Measurement subclass with its configuration.
A model that is not a concrete Sample or Measurement subclass raises
ConfigurationError.Args: model_class: The model class to register. config: The configuration instance for the model. A default one is built when omitted.
Raises: DuplicateRegistrationError: The model is already registered.
- register_admin(model_class: type[django.db.models.Model], config_instance: fairdm.registry.config.ModelConfiguration) None[source]#
Register the model’s admin class with the Django admin site.
A model already present in the admin site is left alone. A portal that wrote
@admin.register(RockSample)has said which admin class it wants, and the registry does not overrule that. Autodiscovery runs before registration, so this is the normal path for any portal with a hand-written admin.Every other failure propagates.
Args: model_class: The model to register. config_instance: The configuration that supplies the admin class.
- get_config(model_class: type[django.db.models.Model], config: fairdm.registry.config.ModelConfiguration | type[fairdm.registry.config.ModelConfiguration] | None = None) fairdm.registry.config.ModelConfiguration[source]#
Build the configuration instance for a model.
Args: model_class: The Django model class. config: A configuration class, an instance, or
Nonefor the registered configuration or a default one.Returns: The configuration instance.
- registry#
‘FairDMRegistry(…)’
- register(config_cls: type) type[source]#
Register a Sample or Measurement model with its configuration.
The configuration class must have a
modelattribute pointing to the Sample or Measurement subclass to register. A model that is not a concrete subclass of either raisesConfigurationError.Usage: @fairdm.register class MySampleConfig(SampleConfig): model = MySample display_name = “My Sample Type” list_fields = [“name”, “created”]
Args: config_cls: The configuration class to register.
Returns: The configuration class, unchanged.
Raises: ValueError: The configuration class has no
modelattribute.