For the List View: More Control and Information These features enhance the main list of your records, allowing for quick actions and better at-a-glance information. list_editable ✏️ This is a game-changer for quick updates. It allows you to make fields editable directly on the list view, so you don't have to …
For the List View: More Control and Information
These features enhance the main list of your records, allowing for quick actions and better at-a-glance information.
list_editable
✏️
This is a game-changer for quick updates. It allows you to make fields editable directly on the list view, so you don't have to click into each item individually. For it to work, the field must also be listed in list_display
.
Use Case: Quickly changing the status or completion state of multiple tasks at once.
# admin.py
from django.contrib import admin
from .models import Task
@admin.register(Task)
class TaskAdmin(admin.ModelAdmin):
list_display = ('title', 'status', 'completed', 'due_date')
list_editable = ('status', 'completed') # Users can edit these two fields on the list page
list_filter
🔍
This adds a filtering sidebar to the list view. It’s perfect for narrowing down large sets of data. Django automatically provides sensible filter options for field types like dates, booleans, and foreign keys.
Use Case: Finding all "In Progress" tasks or tasks belonging to a specific "Epic."
# admin.py
@admin.register(Task)
class TaskAdmin(admin.ModelAdmin):
list_display = ('title', 'status', 'epic')
list_filter = ('status', 'epic') # Adds a filter sidebar for these fields
search_fields
🔎
This adds a search box that allows you to perform a LIKE
query across multiple fields. You can also traverse relationships using the __
notation.
Use Case: Searching for tasks by their title, description, or the name of their parent epic.
# admin.py
@admin.register(Task)
class TaskAdmin(admin.ModelAdmin):
search_fields = ('title', 'description', 'epic__name')
Custom Admin Actions ⚡
You can create your own actions that appear in the "Actions" dropdown menu. This allows you to perform bulk updates on selected records.
Use Case: Select multiple tasks and mark them all as "Completed."
# admin.py
from django.contrib import admin
from .models import Task
def mark_as_completed(modeladmin, request, queryset):
queryset.update(completed=True, status='Done')
mark_as_completed.short_description = "Mark selected tasks as Completed"
@admin.register(Task)
class TaskAdmin(admin.ModelAdmin):
list_display = ('title', 'status', 'completed')
actions = [mark_as_completed]
For the Detail (Form) View: Better Organization
These features improve the form you use to edit a single object.
fieldsets
📂
This feature lets you organize the fields on your form into logical groups. You can even make these groups collapsible for a cleaner interface, which is fantastic for models with many fields.
Use Case: Grouping an Epic's main details separately from its scheduling and budget information.
# admin.py
from django.contrib import admin
from .models import Epic
@admin.register(Epic)
class EpicAdmin(admin.ModelAdmin):
fieldsets = (
('Core Information', {
'fields': ('name', 'description')
}),
('Scheduling', {
'fields': ('start_date', 'end_date'),
'classes': ('collapse',) # This section will be collapsible
}),
)
raw_id_fields
🆔
When a ForeignKey
or ManyToManyField
has hundreds or thousands of options, the standard dropdown menu can kill your page's performance. raw_id_fields
replaces it with a simple text input for the object's ID and a magnifying glass icon to open a search pop-up.
Use Case: Assigning a task to an epic when you have thousands of epics.
# admin.py
from django.contrib import admin
from .models import Task
@admin.register(Task)
class TaskAdmin(admin.ModelAdmin):
raw_id_fields = ('epic',)
filter_horizontal
and filter_vertical
↔️
For ManyToManyField
relationships, these options provide a much more user-friendly interface than the default multi-select box. They create a two-paned widget that lets you easily search and move items between "available" and "chosen" lists.
Use Case: Assigning multiple users to an Epic.
# admin.py
from django.contrib import admin
from .models import Epic
@admin.register(Epic)
class EpicAdmin(admin.ModelAdmin):
# Assumes 'assignees' is a ManyToManyField on the Epic model
filter_horizontal = ('assignees',) # Or use filter_vertical for a stacked layout