Plain is headed towards 1.0! Subscribe for development updates →

plain.models

Model your data and store it in a database.

# app/users/models.py
from plain import models
from plain.passwords.models import PasswordField


class User(models.Model):
    email = models.EmailField(unique=True)
    password = PasswordField()
    is_staff = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.email

Create, update, and delete instances of your models:

from .models import User


# Create a new user
user = User.objects.create(
    email="[email protected]",
    password="password",
)

# Update a user
user.email = "[email protected]"
user.save()

# Delete a user
user.delete()

# Query for users
staff_users = User.objects.filter(is_staff=True)

Installation

# app/settings.py
INSTALLED_PACKAGES = [
    ...
    "plain.models",
]

To connect to a database, you can provide a DATABASE_URL environment variable.

DATABASE_URL=postgresql://user:password@localhost:5432/dbname

Or you can manually define the DATABASES setting.

# app/settings.py
DATABASES = {
    "default": {
        "ENGINE": "plain.models.backends.postgresql",
        "NAME": "dbname",
        "USER": "user",
        "PASSWORD": "password",
        "HOST": "localhost",
        "PORT": "5432",
    }
}

Multiple backends are supported, including Postgres, MySQL, and SQLite.

Querying

Migrations

Migration docs

Fields

Field docs

Validation

Indexes and constraints

Managers

Forms

 1from os import environ
 2
 3from . import database_url
 4
 5# Make DATABASES a required setting
 6DATABASES: dict
 7
 8# Automatically configure DATABASES if a DATABASE_URL was given in the environment
 9if "DATABASE_URL" in environ:
10    DATABASES = {
11        "default": database_url.parse(
12            environ["DATABASE_URL"],
13            # Enable persistent connections by default
14            conn_max_age=int(environ.get("DATABASE_CONN_MAX_AGE", 600)),
15            conn_health_checks=environ.get(
16                "DATABASE_CONN_HEALTH_CHECKS", "true"
17            ).lower()
18            in [
19                "true",
20                "1",
21            ],
22        )
23    }
24
25# Classes used to implement DB routing behavior.
26DATABASE_ROUTERS = []
27
28# The tablespaces to use for each model when not specified otherwise.
29DEFAULT_TABLESPACE = ""
30DEFAULT_INDEX_TABLESPACE = ""