I have this class ShiftAdmin:
class ShiftAdmin(admin.ModelAdmin):
list_display = ['id', 'start_date', 'formatted_start_time', 'formatted_end_time', 'shift_title', 'site', 'user', 'status']
actions = ['delete_selected', 'export_selected_to_csv']
def delete_selected(self, request, queryset):
queryset.delete()
delete_selected.short_description = "Delete selected"
def export_selected_to_csv(self, request, queryset):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="shifts.csv"'
writer = csv.writer(response, delimiter=';')
writer.writerow(['ID', 'Start Date', 'Start Time', 'End Time', 'Shift Title', 'Site ID', 'User ID', 'Status'])
for shift in queryset:
writer.writerow([shift.id, shift.start_date, shift.start_time.strftime('%H:%M'), shift.end_time.strftime('%H:%M'),
shift.shift_title, shift.site_id, shift.user_id, shift.status])
return response
export_selected_to_csv.short_description = "Export selected to CSV"
def formatted_start_time(self, obj):
return obj.start_time.strftime('%H:%M')
formatted_start_time.short_description = 'Start Time'
def formatted_end_time(self, obj):
return obj.end_time.strftime('%H:%M')
formatted_end_time.short_description = 'End Time'
admin.site.register(Shift, ShiftAdmin)
I would like to add a search option that looks in all the fields. The date format should be 12-02-2023
Would that be possible?
EG: search_fields = [‘id’, ‘start_date’, ‘start_time’, ‘end_time’, ‘shift_title’, ‘site__site_name’, ‘user__first_name’,
‘user__last_name’]