Skip to main content

Simplifies the use of function attributes for the django admin and makes mypy happy

Project description

django-admin-display

Version Build status Coverage Python Versions License

Simplifies the use of function attributes (eg. short_description) for the django admin and makes mypy happy :)

Requirements

  • Python >= 3.6
  • Django >= 1.11

Usage

If you want to change the behaviour of how Django displays a read-only value in the admin interface, you can add some special attributes to the corresponding method. Supported values are

short_description
Customize the column’s title of the callable.

empty_value_display
Show this value instead, if the value of a field is None, an empty string, or an iterable without elements.

admin_order_field
Indicate that the value is represented by a certain database field.

boolean
Display a pretty “on” or “off” icon if the method returns a boolean.

allow_tags (deprecated since Django 1.9)
Disable auto-escaping.

The following example shows, how you normally apply these attributes to an AdminModel or a Model method.

class Company(models.Model):
    ...

    def owner(self) -> bool:
        return self.owner.last_name
    owner.short_description = "Company owner"
    owner.admin_order_field = 'owner__last_name'

This module replaces the way of defining these attributes by providing a handy decorator.

from django_admin_display import admin_display


class Company(models.Model):
    ...

    @admin_display(
        short_description="Company owner",
        admin_order_field='owner__last_name',
    )
    def owner(self) -> bool:
        return self.owner.last_name

Why?

There are mainly two reasons why this module exists.

Usage with @property

It is quite common that a calculated model property is displayed in the admin interface:

class Company(models.Model):
    ...

    @property
    def created_on(self) -> datetime.date:
        return self.created_at.date()

In order to add special attributes, you have to create a protected method, attach the attributes and wrap that method using property():

class Company(models.Model):
    ...

    def _created_on(self) -> datetime.date:
        return self.created_at.date()
    _created_on.short_description = "Created on"
    created_on = property(_created_on)

This is quite cumbersome, hard to read and most people don't know that this is even possible. To overcome these downsides you can achieve the same result using the @admin_display decorator:

from django_admin_display import admin_display


class Company(models.Model):
    ...

    @property
    @admin_display(
        short_description = "Created on",
    )
    def created_on(self) -> datetime.date:
        return self.created_at.date()

mypy

If you are using mypy, you have probably stumbled over an error similar to this one

"Callable[[Any, Any], Any]" has no attribute "short_description"

A common solution is to ignore the type checking by adding # type: ignore to the end of the line:

class CompanyAdmin(admin.ModelAdmin):
    ...

    def created_on(self, company: models.Company) -> datetime.date:
        return company.created_at.date()
    created_on.short_description = "Created on"  # type: ignore

The issue is already known and heavily discussed on github.

This decorator solves the issue by internally using # type: ignore and providing a well-defined signature for setting the attributes. It is not an optimal solution but works well until the issue has been resolved.

Development

This project is using poetry to manage all dev dependencies. Clone this repository and run:

poetry install
poetry run pre-commit install
poetry run pip install Django

to create a virtual environment with all dependencies. You can now run the test suite using:

poetry run pytest

This repository follows the angular commit conventions.

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-admin-display-1.2.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

django_admin_display-1.2.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file django-admin-display-1.2.0.tar.gz.

File metadata

  • Download URL: django-admin-display-1.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for django-admin-display-1.2.0.tar.gz
Algorithm Hash digest
SHA256 2f99ad28a0824032851e10dc4ae55d6796b495cd118368bd367cb421a05b41f1
MD5 df9fef19837bcf80fe9ffa5f5b8d8ff4
BLAKE2b-256 b23f622ea8e3dd5e02582236e2ba1f1faa340b610fe2de7e73e7d13972936397

See more details on using hashes here.

File details

Details for the file django_admin_display-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_admin_display-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for django_admin_display-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 473abc224be55624923abc93d369faaa7175df3cda11be2c6c7b8731769fef94
MD5 574bc201ba6b46fcfa3613475824fd25
BLAKE2b-256 2f83180c3eb719bbb55e6b460c3c8b609bd63a365dc7b6234adfeacbde16d0a7

See more details on using hashes here.

Supported by

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