Package: django-admin-display
django-admin-displaySimplifies the use of function attributes (eg.short_description) for the django admin and makes mypy happy :)RequirementsPython 3.6.1 or newerDjango >= 1.11UsageIf you want to change the behaviour of how Django displays a read-only value in the admin interface,
you can add somespecial attributesto the corresponding method.
Supported values areshort_descriptionCustomize the column’s title of the callable.empty_value_displayShow this value instead, if the value of a field isNone, an empty string, or an iterable without elements.admin_order_fieldIndicate that the value is represented by a certain database field.booleanDisplay 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 anAdminModelor aModelmethod.classCompany(models.Model):...defowner(self)->bool:returnself.owner.last_nameowner.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.fromdjango_admin_displayimportadmin_displayclassCompany(models.Model):...@admin_display(short_description="Company owner",admin_order_field='owner__last_name',)defowner(self)->bool:returnself.owner.last_nameWhy?There are mainly two reasons why this module exists.Usage with@propertyIt is quite common that a calculated model property is displayed in the admin interface:classCompany(models.Model):...@propertydefcreated_on(self)->datetime.date:returnself.created_at.date()In order to add special attributes, you have to create a protected method,
attach the attributes and wrap that method usingproperty():classCompany(models.Model):...def_created_on(self)->datetime.date:returnself.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_displaydecorator:fromdjango_admin_displayimportadmin_displayclassCompany(models.Model):...@property@admin_display(short_description="Created on",)defcreated_on(self)->datetime.date:returnself.created_at.date()mypyIf you are usingmypy, 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: ignoreto the end of the line:classCompanyAdmin(admin.ModelAdmin):...defcreated_on(self,company:models.Company)->datetime.date:returncompany.created_at.date()created_on.short_description="Created on"# type: ignoreThe issue is already known and heavily discussed ongithub.This decorator solves the issue by internally using# type: ignoreand providing a well-defined signature for setting the attributes.
It is not an optimal solution but works well until the issue has been resolved.DevelopmentThis project usespoetryfor packaging and
managing all dependencies andpre-committo runflake8,isort,mypyandblack.Clone this repository and runpoetryinstall
poetryrunpre-commitinstallto create a virtual enviroment containing all dependencies.
Afterwards, you can run the test suite usingpoetryrunpytestThis repository follows theConventional Commitsstyle.Cookiecutter templateThis project was created usingcruftand thecookiecutter-pyprojecttemplate.
In order to update this repository to the latest template version runcruftupdatein the root of this repository.