5.2. Admin Fields
autocomplete_fields
exclude
fieldsets
inlines
list_display
list_display_links
list_filter
ordering
radio_fields
readonly_fields
search_fields
list_per_page
5.2.1. SetUp
>>> from django.contrib import admin
>>> from django.utils.translation import gettext_lazy as _
>>> from shop.models import Customer, Address, Product, Order
5.2.2. autocomplete_fields
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... autocomplete_fields = ['addresses']
5.2.3. exclude
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... exclude = ['add_date', 'edit_date']
5.2.4. fieldsets
list[tuple[str, dict]]
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... fieldsets = [
... (None, {'fields': ['is_verified']}),
... (_('Personal'), {'fields': ['firstname', 'lastname', 'birthdate', 'gender'], 'classes': ['fieldset_personal']}),
... (_('Financial'), {'fields': ['tax_number'], 'classes': ['fieldset_financial']}),
... (_('Contact'), {'fields': ['email', 'phone'], 'classes': ['fieldset_contact']}),
... (_('Other'), {'fields': ['image'], 'classes': ['fieldset_other']}),
... ]
5.2.5. list_display
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... list_display = ['lastname', 'firstname', 'birthdate']
5.2.6. list_display_links
Django by default uses the first field in list_display as the link to the change page
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... list_display_links = ['lastname', 'firstname']
5.2.7. list_filter
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... list_filter = ['birthdate', 'gender']
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... list_filter = ['birthdate', 'gender']
...
... def get_list_filter(self, request):
... fields = super().get_list_filter(request)
... if request.user.is_superuser:
... fields = ['is_active'] + fields
... return fields
5.2.8. ordering
-fieldname
- descending order
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... ordering = ['-lastname', 'firstname']
5.2.9. radio_fields
radio_fields = {'fieldname': ...}
admin.VERTICAL
admin.HORIZONTAL
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... radio_fields = {'gender': admin.VERTICAL}
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... radio_fields = {'gender': admin.HORIZONTAL}
5.2.10. readonly_fields
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... readonly_fields = ['add_date', 'edit_date']
5.2.11. search_fields
search_fields
^
- starts with=
- exactly@
- contains
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... search_fields = ['lastname', 'firstname']
>>>
... @admin.register(Address)
... class AddressAdmin(admin.ModelAdmin):
... search_fields = ['^customer__lastname', '@street', '=postcode']
>>>
... @admin.register(Product)
... class ProductAdmin(admin.ModelAdmin):
... search_fields = ['^name', '=barcode']
>>>
... @admin.register(Order)
... class OrderAdmin(admin.ModelAdmin):
... search_fields = ['^customer__lastname', '^product__name']
5.2.12. list_per_page
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... list_per_page = 100
5.2.13. Custom Field Display
>>>
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... list_display = ['firstname', 'lastname', 'display_emails']
...
... @admin.display(empty_value='', description=_('Email'))
... def display_emails(self, model):
... result = model.emails.values_list('email', flat=True)
... return ', '.join(result)