mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #16117 -- Added decorators for admin action and display functions.
Refs #25134, #32099.
This commit is contained in:
@@ -49,6 +49,7 @@ from .models import (
|
||||
)
|
||||
|
||||
|
||||
@admin.display(ordering='date')
|
||||
def callable_year(dt_value):
|
||||
try:
|
||||
return dt_value.year
|
||||
@@ -56,9 +57,6 @@ def callable_year(dt_value):
|
||||
return None
|
||||
|
||||
|
||||
callable_year.admin_order_field = 'date'
|
||||
|
||||
|
||||
class ArticleInline(admin.TabularInline):
|
||||
model = Article
|
||||
fk_name = 'section'
|
||||
@@ -138,25 +136,24 @@ class ArticleAdmin(ArticleAdminWithExtraUrl):
|
||||
|
||||
# These orderings aren't particularly useful but show that expressions can
|
||||
# be used for admin_order_field.
|
||||
@admin.display(ordering=models.F('date') + datetime.timedelta(days=3))
|
||||
def order_by_expression(self, obj):
|
||||
return obj.model_year
|
||||
order_by_expression.admin_order_field = models.F('date') + datetime.timedelta(days=3)
|
||||
|
||||
@admin.display(ordering=models.F('date'))
|
||||
def order_by_f_expression(self, obj):
|
||||
return obj.model_year
|
||||
order_by_f_expression.admin_order_field = models.F('date')
|
||||
|
||||
@admin.display(ordering=models.F('date').asc(nulls_last=True))
|
||||
def order_by_orderby_expression(self, obj):
|
||||
return obj.model_year
|
||||
order_by_orderby_expression.admin_order_field = models.F('date').asc(nulls_last=True)
|
||||
|
||||
def changelist_view(self, request):
|
||||
return super().changelist_view(request, extra_context={'extra_var': 'Hello!'})
|
||||
|
||||
@admin.display(ordering='date', description=None)
|
||||
def modeladmin_year(self, obj):
|
||||
return obj.date.year
|
||||
modeladmin_year.admin_order_field = 'date'
|
||||
modeladmin_year.short_description = None
|
||||
|
||||
def delete_model(self, request, obj):
|
||||
EmailMessage(
|
||||
@@ -216,6 +213,7 @@ class ThingAdmin(admin.ModelAdmin):
|
||||
class InquisitionAdmin(admin.ModelAdmin):
|
||||
list_display = ('leader', 'country', 'expected', 'sketch')
|
||||
|
||||
@admin.display
|
||||
def sketch(self, obj):
|
||||
# A method with the same name as a reverse accessor.
|
||||
return 'list-display-sketch'
|
||||
@@ -280,6 +278,7 @@ class SubscriberAdmin(admin.ModelAdmin):
|
||||
SubscriberAdmin.overridden = True
|
||||
super().delete_queryset(request, queryset)
|
||||
|
||||
@admin.action
|
||||
def mail_admin(self, request, selected):
|
||||
EmailMessage(
|
||||
'Greetings from a ModelAdmin action',
|
||||
@@ -289,6 +288,7 @@ class SubscriberAdmin(admin.ModelAdmin):
|
||||
).send()
|
||||
|
||||
|
||||
@admin.action(description='External mail (Another awesome action)')
|
||||
def external_mail(modeladmin, request, selected):
|
||||
EmailMessage(
|
||||
'Greetings from a function action',
|
||||
@@ -298,32 +298,23 @@ def external_mail(modeladmin, request, selected):
|
||||
).send()
|
||||
|
||||
|
||||
external_mail.short_description = 'External mail (Another awesome action)'
|
||||
|
||||
|
||||
@admin.action(description='Redirect to (Awesome action)')
|
||||
def redirect_to(modeladmin, request, selected):
|
||||
from django.http import HttpResponseRedirect
|
||||
return HttpResponseRedirect('/some-where-else/')
|
||||
|
||||
|
||||
redirect_to.short_description = 'Redirect to (Awesome action)'
|
||||
|
||||
|
||||
@admin.action(description='Download subscription')
|
||||
def download(modeladmin, request, selected):
|
||||
buf = StringIO('This is the content of the file')
|
||||
return StreamingHttpResponse(FileWrapper(buf))
|
||||
|
||||
|
||||
download.short_description = 'Download subscription'
|
||||
|
||||
|
||||
@admin.action(description='No permission to run')
|
||||
def no_perm(modeladmin, request, selected):
|
||||
return HttpResponse(content='No permission to perform this action', status=403)
|
||||
|
||||
|
||||
no_perm.short_description = 'No permission to run'
|
||||
|
||||
|
||||
class ExternalSubscriberAdmin(admin.ModelAdmin):
|
||||
actions = [redirect_to, external_mail, download, no_perm]
|
||||
|
||||
@@ -441,6 +432,7 @@ class LinkInline(admin.TabularInline):
|
||||
|
||||
readonly_fields = ("posted", "multiline", "readonly_link_content")
|
||||
|
||||
@admin.display
|
||||
def multiline(self, instance):
|
||||
return "InlineMultiline\ntest\nstring"
|
||||
|
||||
@@ -501,19 +493,22 @@ class PostAdmin(admin.ModelAdmin):
|
||||
LinkInline
|
||||
]
|
||||
|
||||
@admin.display
|
||||
def coolness(self, instance):
|
||||
if instance.pk:
|
||||
return "%d amount of cool." % instance.pk
|
||||
else:
|
||||
return "Unknown coolness."
|
||||
|
||||
@admin.display(description='Value in $US')
|
||||
def value(self, instance):
|
||||
return 1000
|
||||
value.short_description = 'Value in $US'
|
||||
|
||||
@admin.display
|
||||
def multiline(self, instance):
|
||||
return "Multiline\ntest\nstring"
|
||||
|
||||
@admin.display
|
||||
def multiline_html(self, instance):
|
||||
return mark_safe("Multiline<br>\nhtml<br>\ncontent")
|
||||
|
||||
@@ -655,9 +650,9 @@ class ComplexSortedPersonAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', 'age', 'is_employee', 'colored_name')
|
||||
ordering = ('name',)
|
||||
|
||||
@admin.display(ordering='name')
|
||||
def colored_name(self, obj):
|
||||
return format_html('<span style="color: #ff00ff;">{}</span>', obj.name)
|
||||
colored_name.admin_order_field = 'name'
|
||||
|
||||
|
||||
class PluggableSearchPersonAdmin(admin.ModelAdmin):
|
||||
@@ -706,20 +701,18 @@ class AdminOrderedModelMethodAdmin(admin.ModelAdmin):
|
||||
|
||||
|
||||
class AdminOrderedAdminMethodAdmin(admin.ModelAdmin):
|
||||
@admin.display(ordering='order')
|
||||
def some_admin_order(self, obj):
|
||||
return obj.order
|
||||
some_admin_order.admin_order_field = 'order'
|
||||
ordering = ('order',)
|
||||
list_display = ('stuff', 'some_admin_order')
|
||||
|
||||
|
||||
@admin.display(ordering='order')
|
||||
def admin_ordered_callable(obj):
|
||||
return obj.order
|
||||
|
||||
|
||||
admin_ordered_callable.admin_order_field = 'order'
|
||||
|
||||
|
||||
class AdminOrderedCallableAdmin(admin.ModelAdmin):
|
||||
ordering = ('order',)
|
||||
list_display = ('stuff', admin_ordered_callable)
|
||||
@@ -814,6 +807,7 @@ class UnchangeableObjectAdmin(admin.ModelAdmin):
|
||||
return [p for p in urlpatterns if p.name and not p.name.endswith("_change")]
|
||||
|
||||
|
||||
@admin.display
|
||||
def callable_on_unknown(obj):
|
||||
return obj.unknown
|
||||
|
||||
@@ -831,21 +825,27 @@ class MessageTestingAdmin(admin.ModelAdmin):
|
||||
actions = ["message_debug", "message_info", "message_success",
|
||||
"message_warning", "message_error", "message_extra_tags"]
|
||||
|
||||
@admin.action
|
||||
def message_debug(self, request, selected):
|
||||
self.message_user(request, "Test debug", level="debug")
|
||||
|
||||
@admin.action
|
||||
def message_info(self, request, selected):
|
||||
self.message_user(request, "Test info", level="info")
|
||||
|
||||
@admin.action
|
||||
def message_success(self, request, selected):
|
||||
self.message_user(request, "Test success", level="success")
|
||||
|
||||
@admin.action
|
||||
def message_warning(self, request, selected):
|
||||
self.message_user(request, "Test warning", level="warning")
|
||||
|
||||
@admin.action
|
||||
def message_error(self, request, selected):
|
||||
self.message_user(request, "Test error", level="error")
|
||||
|
||||
@admin.action
|
||||
def message_extra_tags(self, request, selected):
|
||||
self.message_user(request, "Test tags", extra_tags="extra_tag")
|
||||
|
||||
@@ -1156,9 +1156,9 @@ class ArticleAdmin6(admin.ModelAdmin):
|
||||
)
|
||||
sortable_by = ('date', callable_year)
|
||||
|
||||
@admin.display(ordering='date')
|
||||
def modeladmin_year(self, obj):
|
||||
return obj.date.year
|
||||
modeladmin_year.admin_order_field = 'date'
|
||||
|
||||
|
||||
class ActorAdmin6(admin.ModelAdmin):
|
||||
|
||||
Reference in New Issue
Block a user