Skip to main content

Rails-style console ergonomics for Django — pretty printing, model shortcuts, and schema inspection

Project description

django-shellcraft

Rails-style console ergonomics for Django — pretty printing, model shortcuts, and schema inspection.

Built for developers migrating from Ruby on Rails who miss the rails console experience.


Installation

pip install django-shellcraft

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "shellcraft",
]

Usage

python manage.py shellcraft

All models are auto-imported into the shell namespace. IPython is used when available, falling back to the standard Python REPL.

Note: shellcraft refuses to start when DEBUG = False as a production safety guard.


Features

Model shortcuts

All shortcuts are injected as classmethods/instance methods at shell startup. Raw User.objects.all() is unchanged — pretty printing only happens through shellcraft methods.

Method Equivalent
User.find(1) User.objects.get(pk=1)
User.all() User.objects.all() + pretty print
User.where(is_active=True) User.objects.filter(...) + pretty print
User.first() User.objects.first() + pretty print
User.last() User.objects.last() + pretty print
User.count() User.objects.count()
user.update(email="x@y.com") sets attrs + save()
user.destroy() user.delete()
User.fields() colored schema inspection
tables() list all models across all apps

Pretty printing

Single record:

#<User:0x7f3a> {
    :id         => 1,
    :username   => "siva",
    :email      => "siva@example.com",
    :is_active  => true,
    :created_at => 2024-01-15 10:30:00,
}

Multiple records:

[
    [0] #<User:0x7f3a> {
            :id       => 1,
            :username => "siva",
        },
    [1] #<User:0x7f3b> {
            :id       => 2,
            :username => "kumar",
        }
]
2 records

Color mapping:

Type Color
int, float blue
str yellow
True green
False, None red
datetime, date cyan
field types cyan
record index [0] gray

Schema inspection

User.fields()
User {
    :id          AutoField
    :username    CharField
    :email       CharField
    :is_active   BooleanField
    :created_at  DateTimeField
    :group_id    ForeignKey     null: true
}

List all tables

tables()
  auth.User
  auth.Group
  auth.Permission
  myapp.Post
  myapp.Comment

5 tables

Manual pretty printing

ap() is available directly for any model instance or queryset:

ap(User.objects.filter(is_staff=True))

Requirements

  • Python 3.8+
  • Django 3.2, 4.0, 4.1, 4.2, or 5.0
  • django-extensions >= 3.0

Optional: ipython for an enhanced shell experience.


Development

git clone https://github.com/yourusername/django-shellcraft
cd django-shellcraft
pip install -e ".[dev]"
pytest

License

MIT

Project details


Download files

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

Source Distribution

django_shellcraft-0.1.1.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

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

django_shellcraft-0.1.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file django_shellcraft-0.1.1.tar.gz.

File metadata

  • Download URL: django_shellcraft-0.1.1.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for django_shellcraft-0.1.1.tar.gz
Algorithm Hash digest
SHA256 909b1eb7b7afe237b1ddc380c0e614dac1ce5440f28eb61c1b4372a6c6f497fd
MD5 ab9b9bf058a9038d9bf1ace13f1df7c8
BLAKE2b-256 4e27148579b828f26a131d718a5ea635687eeb63e26beb459a27c2e5e20a328d

See more details on using hashes here.

File details

Details for the file django_shellcraft-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_shellcraft-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5f69454baf03e2e7b9464d776cad7fde304d6cacd55313f363e3334a751a1d4a
MD5 3078491aa9de1589b338fa8dc6f1a221
BLAKE2b-256 781cfaf24766f8c7042f902ab1b9a724c8ae6fa596d3394912fff6913ef6b891

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