>>>
... from django.contrib import admin
... from django.utils.translation import gettext_lazy as _
...
...
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... ordering = ['lastname', 'firstname']
... list_display = ['lastname', 'firstname', 'birthdate', 'field_age']
... list_display_links = ['lastname']
... search_fields = ['^lastname']
... list_filter = ['created_date', 'modified_date', 'gender']
... exclude = ['uuid', 'reporter', 'is_active']
... readonly_fields = ['created_date', 'modified_date']
... autocomplete_fields = ['friends']
... fieldsets = [
... (_('Personal Data'), {'fields': ['lastname', 'firstname', 'birthdate', 'gender']}),
... (_('Additional Data'), {'fields': ['email', 'bio', 'image']}),
... (_('Relations'), {'fields': ['status', 'friends']})]
... radio_fields = {
... 'gender': admin.HORIZONTAL,
... 'status': admin.VERTICAL}
...
... def get_list_display(self, request):
... list_display = super().get_list_display(request)
... if request.user.is_superuser and 'is_active' not in list_display:
... list_display.insert(0, 'is_active')
... return list_display
...
... def get_queryset(self, request):
... queryset = super().get_queryset(request)
... if not request.user.is_superuser:
... queryset = queryset.filter(is_active=False)
... return queryset
...
... @admin.display(description=_('Age'), ordering='birthdate', empty_value=_('Unknown'))
... def field_age(self, obj):
... age = obj.get_age()
... return str(age) if age else ''
...
... def save_model(self, request, obj, form, change):
... obj.reporter = request.user
... super().save_model(request, obj, form, change)
...
... class Media:
... js = ['contact/js/alert.js']
... css = {'all': ['contact/css/style.css']}