# Use Django ORM

> Query and update the Cloud Management Platform (CMP) database from Python instead of SQL

---

Site [llms.txt](/llms.txt)

---

CMP's models are exposed through Django's object-relational mapper. Rather than looking up table and field names for SQL statements, you express create, read, update, and delete operations as Python, which makes them straightforward to script from a plugin or the interactive shell.

## Queries become Python

For example, this SQL statement:

```sql
SELECT * FROM infrastructure_server;
```

becomes:

```python
Server.objects.all()
```

## Iterate results directly

Instead of loading a recordset from a database driver and stepping through it, you iterate the queryset with ordinary Python constructs.

```python
# The Pythonic way of writing...
# SELECT * FROM infrastructure_server WHERE hostname LIKE 'r%';
# ...and iterating over the result set.

# This filter is case insensitive, so it returns servers starting with "r" or "R"
servers = Server.objects.filter(hostname__istartswith='r')
for server in servers:
    print(server.id, server.hostname)
```

Because the Django runtime is loaded when shell_plus starts, autocomplete works on model names and fields, which makes the ORM easier to explore interactively.

## Related

- [Resources for writing plugins](/docs/cmp/use/orchestration/plugins/resources/)
- [Structure of a plugin](/docs/cmp/use/orchestration/plugins/structure/)
- [Development tips for plugins](/docs/cmp/use/orchestration/plugins/development-tips/)
