0
# Admin Interface
1
2
Django admin integration for browsing, searching, and managing task results through the web interface. Provides comprehensive views for task and group results with configurable read-only or editable access.
3
4
## Capabilities
5
6
### Task Result Administration
7
8
Admin interface for the TaskResult model, providing detailed views and search capabilities for task management.
9
10
```python { .api }
11
class TaskResultAdmin(admin.ModelAdmin):
12
"""Admin interface for TaskResult model."""
13
14
model: type # TaskResult
15
date_hierarchy: str # 'date_done'
16
list_display: tuple # ('task_id', 'periodic_task_name', 'task_name', 'date_done', 'status', 'worker')
17
list_filter: tuple # ('status', 'date_done', 'periodic_task_name', 'task_name', 'worker')
18
readonly_fields: tuple # ('date_created', 'date_started', 'date_done', 'result', 'meta')
19
search_fields: tuple # ('task_name', 'task_id', 'status', 'task_args', 'task_kwargs')
20
fieldsets: tuple # Organized sections for detail view
21
22
def get_readonly_fields(self, request, obj=None):
23
"""
24
Get readonly fields based on DJANGO_CELERY_RESULTS['ALLOW_EDITS'] setting.
25
26
Args:
27
request: Django request object
28
obj: Model instance being edited (None for add)
29
30
Returns:
31
list: Fields that should be read-only
32
"""
33
```
34
35
#### Admin Configuration
36
37
The TaskResultAdmin provides the following interface features:
38
39
**List View:**
40
- Displays: task_id, periodic_task_name, task_name, date_done, status, worker
41
- Filters: status, date_done, periodic_task_name, task_name, worker
42
- Search: task_name, task_id, status, task_args, task_kwargs
43
- Date hierarchy: Organized by date_done for easy navigation
44
45
**Detail View Fieldsets:**
46
47
1. **Basic Information**
48
- task_id, task_name, periodic_task_name
49
- status, worker
50
- content_type, content_encoding
51
52
2. **Parameters**
53
- task_args (task positional arguments)
54
- task_kwargs (task keyword arguments)
55
56
3. **Result Information**
57
- result (task return value)
58
- date_created, date_started, date_done
59
- traceback (if task failed)
60
- meta (task metadata)
61
62
#### Usage Example
63
64
```python
65
# settings.py - Enable admin editing (optional)
66
DJANGO_CELERY_RESULTS = {
67
'ALLOW_EDITS': True # Default: False (read-only)
68
}
69
70
# Access in Django admin at /admin/django_celery_results/taskresult/
71
72
# The admin interface allows you to:
73
# - Browse all task results with pagination
74
# - Filter by status, date, task name, worker
75
# - Search by task ID, name, or arguments
76
# - View detailed task information
77
# - Edit task results (if ALLOW_EDITS is True)
78
```
79
80
### Group Result Administration
81
82
Admin interface for the GroupResult model, providing views for group task management.
83
84
```python { .api }
85
class GroupResultAdmin(admin.ModelAdmin):
86
"""Admin interface for GroupResult model."""
87
88
model: type # GroupResult
89
date_hierarchy: str # 'date_done'
90
list_display: tuple # ('group_id', 'date_done')
91
list_filter: tuple # ('date_done',)
92
readonly_fields: tuple # ('date_created', 'date_done', 'result')
93
search_fields: tuple # ('group_id',)
94
```
95
96
#### Group Admin Configuration
97
98
The GroupResultAdmin provides:
99
100
**List View:**
101
- Displays: group_id, date_done
102
- Filters: date_done
103
- Search: group_id
104
- Date hierarchy: Organized by date_done
105
106
**Detail View:**
107
- All fields with date_created, date_done, result as read-only
108
- Full group result content display
109
110
#### Usage Example
111
112
```python
113
# Access in Django admin at /admin/django_celery_results/groupresult/
114
115
# The admin interface allows you to:
116
# - Browse all group results
117
# - Filter by completion date
118
# - Search by group ID
119
# - View group result details
120
```
121
122
## Configuration
123
124
### Admin Access Control
125
126
Control whether task results can be edited through the admin interface:
127
128
```python
129
# settings.py
130
DJANGO_CELERY_RESULTS = {
131
'ALLOW_EDITS': False # Default: read-only access
132
}
133
134
# When ALLOW_EDITS is False:
135
# - All fields become read-only in admin
136
# - Prevents accidental modification of task results
137
# - Maintains data integrity
138
139
# When ALLOW_EDITS is True:
140
# - Standard fields become editable
141
# - Result, meta, date fields remain read-only
142
# - Allows manual correction of task data
143
```
144
145
### Admin Registration
146
147
The admin classes are automatically registered when the app is installed:
148
149
```python
150
# Automatically registered in django_celery_results.admin
151
from django.contrib import admin
152
from django_celery_results.models import TaskResult, GroupResult
153
154
admin.site.register(TaskResult, TaskResultAdmin)
155
admin.site.register(GroupResult, GroupResultAdmin)
156
```
157
158
### Customizing Admin Views
159
160
You can customize the admin interface by subclassing the provided admin classes:
161
162
```python
163
# myapp/admin.py
164
from django.contrib import admin
165
from django_celery_results.admin import TaskResultAdmin
166
from django_celery_results.models import TaskResult
167
168
class CustomTaskResultAdmin(TaskResultAdmin):
169
# Add custom actions
170
actions = ['mark_as_retry']
171
172
# Customize list display
173
list_display = TaskResultAdmin.list_display + ('custom_field',)
174
175
def mark_as_retry(self, request, queryset):
176
"""Custom admin action to mark tasks for retry."""
177
queryset.update(status='RETRY')
178
self.message_user(request, "Tasks marked for retry")
179
180
mark_as_retry.short_description = "Mark selected tasks for retry"
181
182
# Unregister the default and register custom
183
admin.site.unregister(TaskResult)
184
admin.site.register(TaskResult, CustomTaskResultAdmin)
185
```
186
187
## Security Considerations
188
189
- **Read-only by default**: Prevents accidental modification of task results
190
- **Field-level restrictions**: Sensitive fields (result, meta) remain read-only even when editing is enabled
191
- **Permission-based access**: Respects Django's built-in admin permissions
192
- **Search limitations**: Search fields are carefully selected to avoid performance issues with large datasets
193
194
## Performance Optimization
195
196
The admin interface is optimized for large datasets:
197
198
- **Database indexes**: All list_display and list_filter fields have database indexes
199
- **Pagination**: Large result sets are automatically paginated
200
- **Date hierarchy**: Efficient navigation through time-based data
201
- **Limited search fields**: Search is restricted to indexed fields to maintain performance