Django - Admin - Applying 'label_suffix' for Models
Is to possible to configure 'label_suffix' other than (:) for开发者_StackOverflow社区 all the models in my admin site?
You could create a subclass of the django.contrib.admin.ModelAdmin
class which sets the ModelAdmin.form
's label_suffix
to a set string. That way any model which used that ModelAdmin
would have the same prefix:
# myproject/myapp/admin.py
from django.contrib import admin
from myproject.myapp.models import MyModel, AnotherModel, YetAnotherModel, \
SomeSpecialModel
class PrefixAdmin(admin.ModelAdmin):
def __init__(self, *args, **kwargs):
super(PrefixAdmin, self).__init__(*args, **kwargs)
self.form.label_suffix = 'some suffix here'
# Use this ModelAdmin class for all your models:
admin.site.register(MyModel, PrefixAdmin)
admin.site.register(AnotherModel, PrefixAdmin)
admin.site.register(YetAnotherModel, PrefixAdmin)
# Or if you need a specific ModelAdmin for a particular Model
# just extend from the PrefxiAdmin class:
class SpecialModelAdmin(PrefixAdmin):
# ...
admin.site.register(SomeSpecialModel, SpecialModelAdmin)
label_suffix
doesn't work for Django Admin model forms because the suffix ':' is fixed into the code (see django/contrib/admin/helpers.py
).
精彩评论