paperap.models.abstract.model module
METADATA:
- File: base.py
Project: paperap
- Created: 2025-03-04
Version: 0.0.9
Author: Jess Mann Email: jess@jmann.me
Copyright (c) 2025 Jess Mann
LAST MODIFIED:
2025-03-04 By Jess Mann
- class paperap.models.abstract.model.BaseModel(**data)[source]
-
Base model for all Paperless-ngx API objects.
Provides automatic serialization, deserialization, and API interactions with minimal configuration needed.
- _meta
Metadata for the model, including filtering and resource information.
- _save_lock
Lock for saving operations.
- _pending_save
Future object for pending save operations.
- Raises:
ValueError – If resource is not provided.
- Parameters:
data (
Any)
- class Meta(model)[source]
Bases:
GenericMetadata for the Model.
- name
The name of the model.
- read_only_fields
Fields that should not be modified.
- filtering_disabled
Fields disabled for filtering.
- filtering_fields
Fields allowed for filtering.
- supported_filtering_params
Params allowed during queryset filtering.
- blacklist_filtering_params
Params disallowed during queryset filtering.
- filtering_strategies
Strategies for filtering.
- resource
The BaseResource instance.
- queryset
The type of QuerySet for the model.
- Raises:
ValueError – If both ALLOW_ALL and ALLOW_NONE filtering strategies are set.
- Parameters:
model (type[_Self])
-
filtering_strategies:
ClassVar[set[FilteringStrategies]] = {FilteringStrategies.BLACKLIST}
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'ignore', 'populate_by_name': True, 'use_enum_values': True, 'validate_assignment': True, 'validate_default': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- __init__(**data)[source]
Initialize the model with resource and data.
- Parameters:
resource – The BaseResource instance.
**data (
Any) – Additional data to initialize the model.
- Raises:
ValueError – If resource is not provided.
- property resource: BaseResource[Self, BaseQuerySet]
- property save_executor: ThreadPoolExecutor
- model_post_init(context: Any, /) None
We need to both initialize private attributes and call the user-defined model_post_init method.
- classmethod from_dict(data)[source]
Create a model instance from API response data.
- Parameters:
data (
dict[str,Any]) – Dictionary containing the API response data.- Return type:
Self- Returns:
A model instance initialized with the provided data.
Examples
# Create a Document instance from API data doc = Document.from_dict(api_data)
- to_dict(*, include_read_only=True, exclude_none=False, exclude_unset=True)[source]
Convert the model to a dictionary for API requests.
- Parameters:
- Return type:
- Returns:
A dictionary with model data ready for API submission.
Examples
# Convert a Document instance to a dictionary data = doc.to_dict()
- dirty_fields(comparison='both')[source]
Show which fields have changed since last update from the paperless ngx db.
- Parameters:
comparison (
Literal['saved','db','both']) – Specify the data to compare (‘saved’ or ‘db’). Db is the last data retrieved from Paperless NGX Saved is the last data sent to Paperless NGX to be saved- Returns:
(original_value, new_value)} of fields that have changed since last update from the paperless ngx db.
- Return type:
A dictionary {field
- is_dirty(comparison='both')[source]
Check if any field has changed since last update from the paperless ngx db.
- classmethod create(**kwargs)[source]
Create a new model instance.
- Parameters:
**kwargs (
Any) – Field values to set.- Return type:
Self- Returns:
A new model instance.
Examples
# Create a new Document instance doc = Document.create(filename=”example.pdf”, contents=b”PDF data”)
- update_locally(*, from_db=None, skip_changed_fields=False, **kwargs)[source]
Update model attributes without triggering automatic save.
- update(**kwargs)[source]
Update this model with new values.
Subclasses implement this with auto-saving features. However, base BaseModel instances simply call update_locally.
Examples
# Update a Document instance doc.update(filename=”new_example.pdf”)
- abstractmethod is_new()[source]
Check if this model represents a new (unsaved) object.
- Return type:
- Returns:
True if the model is new, False otherwise.
Examples
# Check if a Document instance is new is_new = doc.is_new()
- should_save_on_write()[source]
Check if the model should save on attribute write, factoring in the client settings.
- Return type:
- matches_dict(data)[source]
Check if the model matches the provided data.
- Parameters:
data (
dict[str,Any]) – Dictionary containing the data to compare.- Return type:
- Returns:
True if the model matches the data, False otherwise.
Examples
# Check if a Document instance matches API data matches = doc.matches_dict(api_data)
- class paperap.models.abstract.model.StandardModel(**data)[source]
-
Standard model for Paperless-ngx API objects with an ID field.
- id
Unique identifier for the model.
- Parameters:
data (
Any)
- id: int
- class Meta(model)[source]
Bases:
MetaMetadata for the StandardModel.
- read_only_fields
Fields that should not be modified.
- supported_filtering_params
Params allowed during queryset filtering.
- Parameters:
model (type[_Self])
- read_only_fields: ClassVar[set[str]] = {'id'}
- supported_filtering_params: ClassVar[set[str]] = {'id', 'id__in', 'limit'}
- blacklist_filtering_params: ClassVar[set[str]] = {}
- field_map: dict[str, str] = {}
- filtering_disabled: ClassVar[set[str]] = {}
- filtering_fields: ClassVar[set[str]] = {'_resource', 'id'}
- model: type[_Self]
- name: str
- property resource: StandardResource[Self, StandardQuerySet]
- update(**kwargs)[source]
Update this model with new values and save changes.
NOTE: new instances will not be saved automatically. (I’m not sure if that’s the right design decision or not)
- refresh()[source]
Refresh the model with the latest data from the server.
- Return type:
- Returns:
True if the model data changes, False on failure or if the data does not change.
- Raises:
ResourceNotFoundError – If the model is not found on Paperless. (e.g. it was deleted remotely)
- save_sync(*, force=False)[source]
Save this model instance synchronously.
Changes are sent to the server immediately, and the model is updated when the server responds.
- Return type:
- Returns:
True if the save was successful, False otherwise.
- Raises:
ResourceNotFoundError – If the resource doesn’t exist on the server
RequestError – If there’s a communication error with the server
PermissionError – If the user doesn’t have permission to update the resource
- Parameters:
force (
bool)
- save_async(*, force=False)[source]
Save this model instance asynchronously.
Changes are sent to the server in a background thread, and the model is updated when the server responds.
- is_new()[source]
Check if this model represents a new (unsaved) object.
- Return type:
- Returns:
True if the model is new, False otherwise.
Examples
# Check if a Document instance is new is_new = doc.is_new()
- __str__()[source]
Human-readable string representation.
- Return type:
- Returns:
A string representation of the model.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'ignore', 'populate_by_name': True, 'use_enum_values': True, 'validate_assignment': True, 'validate_default': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].