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   => "alice",
    :email      => "alice@example.com",
    :is_active  => true,
    :created_at => 2024-01-15 10:30:00,
}

Multiple records:

[
    [0] #<User:0x7f3a> {
            :id       => 1,
            :username => "alice",
        },
    [1] #<User:0x7f3b> {
            :id       => 2,
            :username => "bob",
        }
]
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/sivanandam03/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.2.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.2-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_shellcraft-0.1.2.tar.gz
  • Upload date:
  • Size: 23.0 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.2.tar.gz
Algorithm Hash digest
SHA256 207d3bd22378b78130faf7c34cf9a76634d17ea32b4612ae5d850a92669e0ee3
MD5 e24e0ad2b1ffbee1f971a2bcca3e6fd1
BLAKE2b-256 d023d584750ee0af3f2f6a4e7e72bdf606e86a1d235461d572547e3a6c667323

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_shellcraft-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f78a21bb14e1c508386f205515d4f0a301a4d166d69a4a0d325cabf32a31ccc0
MD5 9c2575e0716789f65a3ead4feb30b13d
BLAKE2b-256 ab966fc6be5436eb760ed0993124186f08da6844927cca270f97bca5dbd89069

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