Metadata Model
Django ORM base model.
- class smarter.lib.django.models.metadata_model.MetaDataModel(*args, **kwargs)[source]
Abstract base model that adds SAM metadata fields to a.
TimestampedModel Django ORM model. These are the the common fields that makeup the Pydantic SAM metadata model, along with timestamp fields for create/modify tracking.
Example Usage:
from smarter.smarter.lib.django.models import MetaDataModel from smarter.apps.account.models import User class MyModel(MetaDataModel): name = models.CharField(max_length=100)
- Parameters:
created_at (Unknown) – Created at
updated_at (Unknown) – Updated at
name (Unknown) – Name. Name in camelCase, e.g., ‘apiKey’, no special characters.
description (Unknown) – Description. A brief description of this resource. Be verbose, but not too verbose.
version (Unknown) – Version. Semantic version in the format MAJOR.MINOR.PATCH, e.g., ‘1.0.0’.
annotations (Unknown) – Annotations. Key-value pairs for annotating this resource.
Relationship fields:
- Parameters:
tags (Unknown) – Tags. Tags for categorizing and organizing this resource.
- clone(new_name, new_version=None)[source]
Clone the model instance with a new name and optional new version.
- Parameters:
- Returns:
A new instance of MetaDataModel with the same field values except for name and version.
- Return type:
- created_at
-
Created at
Timestamp indicating when the model instance was created.
This field is automatically set to the current date and time when the instance is first created. It is indexed in the database for efficient querying.
- Type:
Type
- description
-
Description. A brief description of this resource. Be verbose, but not too verbose.
- Type:
Type
- classmethod get_cached_object(*args, invalidate=False, pk=None, name=None, **kwargs)[source]
Retrieve a model instance by primary key or name, using caching to.
optimize performance. This method is selectively overridden in models that inherit from MetaDataModel to provide class-specific function parameters.
Example usage:
# Retrieve by primary key instance = MyModel.get_cached_object(pk=1) # Retrieve by name instance = MyModel.get_cached_object(name="exampleName")
- Parameters:
- Returns:
The model instance if found, otherwise None.
- Return type:
- classmethod get_cached_objects(invalidate=False, **kwargs)[source]
Retrieve model instances using caching to optimize performance.
This method is selectively overridden in models that inherit from MetaDataModel to provide class-specific function parameters.
Example usage:
# Retrieve all instances instances = MyModel.get_cached_objects()
- rename(new_name)[source]
Rename the model instance with a new name.
- Parameters:
new_name (
str) – The new name for the instance.- Returns:
The updated instance of MetaDataModel with the new name.
- Return type:
- property rfc1034_compliant_name: str | None[source]
Returns a URL-friendly name for the llm_client.
This property returns an RFC 1034-compliant name for the llm_client, suitable for use in URLs and DNS labels.
Example:
self.name = 'Example LLMClient 1' self.rfc1034_compliant_name # 'example-llm_client-1'
- Returns:
The RFC 1034-compliant name, or None if
self.nameis not set.- Return type:
Optional[str]
- save(*args, **kwargs)[source]
Save the model instance to the database, performing validation before the actual save.
This method overrides the default
save()behavior of Django models to ensure that the model is validated by callingvalidate()before any data is written to the database. If validation fails, adjango.core.exceptions.ValidationErroris raised with detailed information about the error, the arguments passed, the model class, and the current field values.- Parameters:
*args – Positional arguments passed to the parent
save()method. These are forwarded to Django’s ORM.**kwargs – Keyword arguments passed to the parent
save()method. These are forwarded to Django’s ORM.
Examples
obj = MyModel(name="Example") obj.save() # Will call validate() before saving
Note
The
validate()method is intended to be overridden in subclasses to provide custom validation logic.If
validate()raises aValidationError, the save operation is aborted and the error is propagated.The error message includes the arguments, keyword arguments, model class, and current field values for easier debugging.
Important
If you override this method in a subclass, always call
super().save(*args, **kwargs)to retain validation and timestamp functionality.If validation fails, no data will be saved to the database.
- tags = <taggit.managers._TaggableManager object>
- property tags_list: list[str][source]
Return the tags as a list of strings.
We assume that @cached_property is more efficient at fetch than @cache_results, all things considered equal, which provides a marginal boost to instances. Meanwhile, the @cache_results is persisted to the Django cache, and thus outlives this instance. Thus, best of both worlds.
- updated_at
-
Updated at
Timestamp indicating when the model instance was last updated.
This field is automatically updated to the current date and time whenever the instance is saved. It is indexed in the database for efficient querying.
- Type:
Type