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.0.tar.gz (80.6 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.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for django_shellcraft-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0a52fdfbaab3a9e0615d0b8b99f55c5ff469f2ded8c5c4e6b714550e209ffbc7
MD5 f3b4ad3b84e23e657e52ca7ea4d37c00
BLAKE2b-256 33c6cf0c9b769ba13227d5ba7242513ed325fdf17d0c6106b6ec4fca931aaa39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_shellcraft-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d10ec5fae7c2535851bf79547438baaa260058b392094de260b91c3ae2db9441
MD5 e5a7fb8eb552732acdc255eb86855541
BLAKE2b-256 66592049998d0db7dc095c61ce163d1996015c15fdd3851d97ce6790e74aeadc

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