Skip to content

MrCocoDev/django-repr

Repository files navigation

Project generated with PyScaffold

PyPI-Server

GitHub Test CI

Monthly Downloads

django-better-repr

Django model reprs for humans!

This project seeks to make reprs of Django models more human-friendly. This project is heavily inspired by https://github.com/dan-passaro/django-auto-repr .

Warning

This library makes no attempts to make eval`ing the generated `repr`s safe. Use `eval at your own risk!

Quick Caveat

If the Django model has not yet been saved then a model won't be equal to the eval of its repr.

from django.db import models


class MyModel(models.Model):
    ...

A = MyModel()
B = eval(repr(A))

assert A == B
>>> AssertionError:

If you need this behavior then make sure to save your models before taking the repr. This behavior is not the fault of this library, and there is realistically no way for this library to change this behavior. This is due to the implementation of Django's Model.__eq__ (model_eq).

Installation

pip install django-better-repr

If you want to automatically improve the repr of all models in your project then add django_better_repr to your installed apps.

# settings.py
INSTALLED_APPS = [
   ...,
   'django_better_repr',
   ...,
]

Want to customize which apps get configured but don't want to decorate each class individually? Head on down to the Configuration section.

How to use:

The repr logic in this library is designed to produce the smallest, most meaningful repr possible for your Django models. That means that fields which aren't set won't show up in the repr. This should reduce noise and let you get the most value out of your reprs.

from django_better_repr import better_repr

@better_repr
class MyDjangoModel(models.Model):
    my_field = models.CharField(max_length=16)

Or, if class inheritance is more your speed:

from django_better_repr.bases import BetterRepr

class MyDjangoModel(BetterRepr, models.Model):
    my_field = models.CharField(max_length=16)

Then load up your favorite shell and run:

MyDjangoModel.objects.create(my_field='Hello, world!')
>>> MyDjangoModel(my_field='Hello, world!')

If your model has a lot of fields then the repr will automatically switch to pretty printing. This can be configured via settings.

Configuration

If you want to customize the behavior of the library, below are all the options.

# settings.py
BETTER_REPR_CONFIG = {
   'ENABLE_MULTILINE_REPRS': True,  # bool (default: True), whether or not to allow multiline reprs
   'SINGLE_LINE_PARTS_LIMIT': 4,  # int (default: 4), the number of fields a repr can have before switching to multi line
   'MULTILINE_WHITESPACE': '\t',  # str (default: '\t'), the whitespace string to use for multiline reprs
   'AUTO_CONFIGURE_INCLUDE_MODELS': [],  # list (default: a sentinel for all models), which models to auto include if the auto configuration application is added to INSTALLED_APPS
   'AUTO_CONFIGURE_EXCLUDE_MODELS': [],  # list (default: []), which models to exclude from the auto setup if the auto configuration application is added to INSTALLED_APPS
}

Making Changes & Contributing

This project uses pre-commit, please make sure to install it before making any changes:

pip install pre-commit
cd django-better-repr
pre-commit install

It is a good idea to update the hooks to the latest version:

pre-commit autoupdate

Note

This project has been set up using PyScaffold 4.5. For details and usage information on PyScaffold see https://pyscaffold.org/.

About

Human readable __repr__ for Django models

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages