7.3. Admin Edit — Python
Change Form customization in Django Admin
excludereadonly_fieldsradio_fieldsautocomplete_fieldsfieldsets
7.3.1. SetUp
>>> from django.contrib import admin >>> from django.utils.translation import gettext_lazy as _ >>> from shop.models import Customer, Address, Product, Order
7.3.2. exclude
>>> # ... @admin.register(MyModel) ... class MyAdmin(admin.ModelAdmin): ... exclude = ['add_date', 'edit_date']
7.3.3. readonly_fields
>>> # ... @admin.register(MyModel) ... class MyAdmin(admin.ModelAdmin): ... readonly_fields = ['add_date', 'edit_date']
7.3.4. radio_fields
radio_fields = {'fieldname': ...}admin.VERTICALadmin.HORIZONTALMust be a
ForeignKeyor haschoicesdefinedNot working with
BooleanFields, unless you definechoicesmanually
>>> # ... @admin.register(MyModel) ... class MyAdmin(admin.ModelAdmin): ... radio_fields = {'gender': admin.VERTICAL}
>>> # ... @admin.register(MyModel) ... class MyAdmin(admin.ModelAdmin): ... radio_fields = {'gender': admin.HORIZONTAL}
7.3.5. autocomplete_fields
>>> # ... @admin.register(MyModel) ... class MyAdmin(admin.ModelAdmin): ... autocomplete_fields = ['addresses']
7.3.6. fieldsets
list[tuple[str, dict]]
>>> # ... @admin.register(MyModel) ... class MyAdmin(admin.ModelAdmin): ... fieldsets = [ ... (None, {'fields': ['is_verified']}), ... (_('Personal'), {'fields': ['firstname', 'lastname', 'birthdate', 'gender'], 'classes': ['expand', 'fieldset_personal']}), ... (_('Financial'), {'fields': ['tax_number'], 'classes': ['expand', 'fieldset_financial']}), ... (_('Contact'), {'fields': ['email', 'phone'], 'classes': ['expand', 'fieldset_contact']}), ... (_('Other'), {'fields': ['image'], 'classes': ['collapse', 'fieldset_other']}), ... ]
7.3.7. Assignments
# doctest: +SKIP_FILE # %% About # - Name: Admin Edit Exclude # - Difficulty: easy # - Lines: 1 # - Minutes: 2 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 0. Use `myproject.myapp` # 1. In admin edit view for `myapp.Person` # 2. Hide the `is_verified` field # %% Polish # 0. Użyj `myproject.myapp` # 1. W widoku edycji admina dla modelu `myapp.Person` # 2. Ukryj pole `is_verified` # %% Hints # - `admin.ModelAdmin` # - `exclude = [...]` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 12), \ 'Python has an is invalid version; expected: `3.12` or newer.' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' import django; django.setup() # %% Types # %% Data # %% Result
# doctest: +SKIP_FILE # %% About # - Name: Admin Edit Readonly Fields # - Difficulty: easy # - Lines: 1 # - Minutes: 2 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 0. Use `myproject.myapp` # 1. In admin edit view for `myapp.Person` # 2. Make `pk` field as read-only # %% Polish # 0. Użyj `myproject.myapp` # 1. W widoku edycji admina dla modelu `myapp.Person` # 2. Ustaw pole `pk` jako tylko do odczytu # %% Hints # - `admin.ModelAdmin` # - `readonly_fields = [...]` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 12), \ 'Python has an is invalid version; expected: `3.12` or newer.' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' import django; django.setup() # %% Types # %% Data # %% Result
# doctest: +SKIP_FILE # %% About # - Name: Admin Edit Radio Fields # - Difficulty: easy # - Lines: 1 # - Minutes: 2 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 0. Use `myproject.myapp` # 1. In admin edit view for `myapp.Person` # 2. Make `gender` as radio buttons (use `admin.VERTICAL`) # %% Polish # 0. Użyj `myproject.myapp` # 1. W widoku edycji admina dla modelu `myapp.Person` # 2. Ustaw pole `gender` jako przyciski radiowe (użyj `admin.VERTICAL`) # %% Hints # - `admin.ModelAdmin` # - `radio_fields = [...]` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 12), \ 'Python has an is invalid version; expected: `3.12` or newer.' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' import django; django.setup() # %% Types # %% Data # %% Result
# doctest: +SKIP_FILE # %% About # - Name: Admin Edit Autocomplete Fields # - Difficulty: easy # - Lines: 1 # - Minutes: 2 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 0. Use `myproject.myapp` # 1. In admin edit view for `myapp.Person` # 2. Make `group` field as autocomplete field # %% Polish # 0. Użyj `myproject.myapp` # 1. W widoku edycji admina dla modelu `myapp.Person` # 2. Ustaw pole `group` jako pole z autouzupełnianiem # %% Hints # - `admin.ModelAdmin` # - `autocomplete_fields = [...]` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 12), \ 'Python has an is invalid version; expected: `3.12` or newer.' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' import django; django.setup() # %% Types # %% Data # %% Result
# doctest: +SKIP_FILE # %% About # - Name: Admin Edit Fieldsets # - Difficulty: easy # - Lines: 1 # - Minutes: 2 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 0. Use `myproject.myapp` # 1. In admin edit view for `myapp.Person` # 2. Add fieldsets: # - None: `id`, `is_verified` (expanded) # - 'Personal Information': `firstname`, `lastname`, `birthdate`, `gender` (expanded) # - 'Contact Information': `email`, `phone`, `tax_number`, `image` (collapsed) # %% Polish # 0. Użyj `myproject.myapp` # 1. W widoku edycji admina dla modelu `myapp.Person` # 2. Dodaj zestawy pól (fieldsets): # - None: `id`, `is_verified` (otwarty) # - 'Personal Information': `firstname`, `lastname`, `birthdate`, `gender` (otwarty) # - 'Contact Information': `email`, `phone`, `tax_number`, `image` (zwinięty) # %% Hints # - `admin.ModelAdmin` # - `fieldsets = [...]` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 12), \ 'Python has an is invalid version; expected: `3.12` or newer.' """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' import django; django.setup() # %% Types # %% Data # %% Result