Skip to main content

Database models for Plain.

Project description

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


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

    def __str__(self):
        return self.email

Every model automatically includes an id field which serves as the primary key. The name id is reserved and can't be used for other fields.

Create, update, and delete instances of your models:

from .models import User


# Create a new user
user = User.objects.create(
    email="test@example.com",
    password="password",
)

# Update a user
user.email = "new@example.com"
user.save()

# Delete a user
user.delete()

# Query for users
admin_users = User.objects.filter(is_admin=True)

Installation

Install plain.models from PyPI, then add it to your INSTALLED_PACKAGES.

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

# app/settings.py
DATABASE = {
    "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

TODO

Migrations

TODO

Fields

TODO

Validation

TODO

Indexes and constraints

TODO

Managers

TODO

Forms

TODO

Sharing fields across models

To share common fields across multiple models, use Python classes as mixins. The final, registered model must inherit directly from models.Model and the mixins should not.

from plain import models


# Regular Python class for shared fields
class TimestampedMixin:
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)


# Models inherit from the mixin AND models.Model
@models.register_model
class User(TimestampedMixin, models.Model):
    email = models.EmailField()
    password = PasswordField()
    is_admin = models.BooleanField(default=False)


@models.register_model
class Note(TimestampedMixin, models.Model):
    content = models.TextField(max_length=1024)
    liked = models.BooleanField(default=False)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plain_models-0.39.1.tar.gz (351.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plain_models-0.39.1-py3-none-any.whl (403.8 kB view details)

Uploaded Python 3

File details

Details for the file plain_models-0.39.1.tar.gz.

File metadata

  • Download URL: plain_models-0.39.1.tar.gz
  • Upload date:
  • Size: 351.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.0

File hashes

Hashes for plain_models-0.39.1.tar.gz
Algorithm Hash digest
SHA256 8138e6644c71a44a5567e9c4385580a8f24134028ad8dfc7f57d2121513bdac8
MD5 7449178acd0306f346db3efc768db53d
BLAKE2b-256 fbfccff9f49fc72836510e70a9abd665377e1c794d109a5904a6b782281d801f

See more details on using hashes here.

File details

Details for the file plain_models-0.39.1-py3-none-any.whl.

File metadata

File hashes

Hashes for plain_models-0.39.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2696bd8708f4e860c6e050b0a0f38b2a8bc319131bd0469a29451066cec6dc54
MD5 ae3f46907245a9981858af38fabe0737
BLAKE2b-256 c2554fef64c44c4af9be6bb3804b9a4bd9cfb614fce9248bbeff5356000422f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page