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.

annotations

JSONField

Annotations. Key-value pairs for annotating this resource.

Type:

Type

clone(new_name, new_version=None)[source]

Clone the model instance with a new name and optional new version.

Parameters:
  • new_name (str) – The name for the cloned instance.

  • new_version (Optional[str]) – The version for the cloned instance. If not provided, it will be the same as the original.

Returns:

A new instance of MetaDataModel with the same field values except for name and version.

Return type:

MetaDataModel

created_at

DateTimeField

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

TextField

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:
  • invalidate (Optional[bool]) – Whether to invalidate the cache for this retrieval.

  • pk (Optional[int]) – The primary key of the model instance to retrieve.

  • name (Optional[str]) – The name of the model instance to retrieve.

Returns:

The model instance if found, otherwise None.

Return type:

MetaDataModel

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()
Parameters:

invalidate (Optional[bool]) – Whether to invalidate the cache for this retrieval.

Returns:

A queryset of all model instances.

Return type:

QuerySet

name

CharField

Name. Name in camelCase, e.g., ‘apiKey’, no special characters.

Type:

Type

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:

MetaDataModel

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.name is 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 calling validate() before any data is written to the database. If validation fails, a django.core.exceptions.ValidationError is 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 a ValidationError, 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.

Returns:

List of tag names.

Return type:

list[str]

updated_at

DateTimeField

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

validate()[source]

Validate the model.

version

CharField

Version. Semantic version in the format MAJOR.MINOR.PATCH, e.g., ‘1.0.0’.

Type:

Type