Skip to content

AI ORM Toolkit

orm_toolkit

get_project_app_labels()

Return sorted app labels for project-local Django apps (excluding third-party).

Filters out apps installed via site-packages, keeping only those whose path is under settings.BASE_DIR.

Returns:

Type Description

list[str]: Sorted app label strings.

get_app_models_summary(allowed_apps=None)

Generate a text summary of project apps and their models for use in LLM prompts.

Parameters:

Name Type Description Default
allowed_apps

Optional list of app labels to include. Defaults to all project apps.

None

Returns:

Name Type Description
str

Multi-line string with one line per app listing its model names.

list_models_and_fields(app_label=None, allowed_apps=None)

List available Django models and their fields.

Parameters:

Name Type Description Default
app_label Optional[str]

Filter to a specific app (e.g. 'properties').

None
allowed_apps Optional[List[str]]

List of app labels to include. Defaults to project apps.

None

Returns:

Type Description
str

JSON string with model schemas.

query_model(model_name, options=None, allowed_apps=None)

Query a Django model using ORM filters and return results as JSON.

Parameters:

Name Type Description Default
model_name str

Model identifier as 'app_label.model_name' (e.g. 'properties.property').

required
options Optional[dict]

Dict with query options. All keys are optional:

  • filters (dict): ORM filter kwargs passed to .filter(). Supports all Django lookups (e.g. {"status": "Active", "expiry_date__lte": "2026-12-31"}).
  • exclude (dict): ORM exclude kwargs passed to .exclude() (e.g. {"status": "Sold"}).
  • fields (list[str]): Field names for .values(). Supports FK traversal via __ notation (e.g. ["id", "tenant__name", "unit__property__address"]). Defaults to all concrete non-M2M fields.
  • order_by (list[str]): Field names for .order_by(). Prefix with - for descending (e.g. ["-created_date", "name"]).
  • limit (int): Maximum number of results to return. Defaults to 50.
  • count_only (bool): If True, return only {"total_count": N} without fetching rows.
  • distinct_field (str): Return distinct values for this single field (e.g. "status"). Returns {"field", "total_count", "values"}.
  • aggregate (dict): Aggregation specifications. Keys are output names, values are dicts with 'func' and 'field' keys. Supported funcs: count, sum, avg, min, max. Example: {"total": {"func": "sum", "field": "amount"}}.
None
allowed_apps Optional[List[str]]

List of app labels permitted for querying. Defaults to project apps (auto-detected from settings.BASE_DIR).

None

Returns:

Name Type Description
str str

JSON string. On success, contains total_count (count before limit),

str

count (rows returned), and results (list of dicts). On error,

str

contains {"error": "message"}.