Use Django ORM

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

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:

SELECT * FROM infrastructure_server;

becomes:

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.

# 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.