The students those who have registered for a particular workshop can be viewed through the admin page. We would also like to take a report of them, that is in the form of a csv file or pdf file. This can be further used for printing the details for manual attendance, or for any record purposes.
To do this, we can add an action inside the WorkshopRegistrationAdmin
class, to download the selected rows as a csv file.
In order to this, these changes needs to be done inside the event/admin.py
file :
import csv
from django.http import HttpResponse
from django.utils.text import slugify
@admin.register(Workshopregistration)
class WorkshopRegistrationAdmin(admin.ModelAdmin):
list_display = ('workshop', 'student', 'registered_date')
search_fields = ('workshop__name', 'student__email')
list_filter = ('registered_date', 'workshop')
autocomplete_fields = ('workshop', 'student')
def get_queryset(self, request):
qs = super().get_queryset(request)
if not request.user.is_superuser:
qs=qs.filter(workshop__department = request.user.hodprofile.department)
return qs
def export_as_csv(self, request, queryse1t, queryset):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = f'attachment; filename={slugify(queryset.model.__name__)}-export.csv'
writer = csv.writer(response)
fields = [field.name for field in queryset.model._meta.fields]
writer.writerow(fields)
for obj in queryset:
row = [getattr(obj, field) for field in fields]
writer.writerow(row)
return response
export_as_csv.short_description = "Export as CSV"
def get_actions(self, request):
actions = super().get_actions(request)
actions['export_as_csv'] = (self.export_as_csv, 'export_as_csv', "Export selected %(verbose_name_plural)s as CSV")
return actions
The above functions takes all the columns that are present in a particular model and allows it to download as a csv file. So after doing this, you could see the same on the admin page.
Other than delete, there is another option to export as csv. When you click on go, it would download a file which would have the following content.
id | workshop | student | registered_date |
---|---|---|---|
2 | CyberGuard | mustafa@edkool.com | 2023-12-12 06:45:30.140151+00:00 |
1 | Chain Craft | mustafa@edkool.com | 2023-12-06 03:06:26.584382+00:00 |
The csv file would contain these data.
The problem here is, we are not getting all the data that we would want in a csv file. Like, we may require some more details such as student name, his email, contact number, and then coming to workshop say we may want to include the venue as well. So in this case we can add the fields that we want and select the particular row values for it.
@admin.register(Workshopregistration)
class WorkshopRegistrationAdmin(admin.ModelAdmin):
list_display = ('workshop', 'student', 'registered_date')
search_fields = ('workshop__name', 'student__email')
list_filter = ('registered_date', 'workshop')
autocomplete_fields = ('workshop', 'student')
def get_queryset(self, request):
qs = super().get_queryset(request)
if not request.user.is_superuser:
qs=qs.filter(workshop__department = request.user.hodprofile.department)
return qs
def export_as_csv(self, request, queryse1t, queryset):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = f'attachment; filename={slugify(queryset.model.__name__)}-export.csv'
writer = csv.writer(response)
fields = ['Workshop', 'Venue', 'Student Name', 'Student Email', 'Student Contact']
writer.writerow(fields)
for obj in queryset:
row = [obj.workshop.workshop_title, obj.workshop.venue, obj.student.first_name, obj.student.email, obj.student.mobile_number]
writer.writerow(row)
return response
export_as_csv.short_description = "Export as CSV"
def get_actions(self, request):
actions = super().get_actions(request)
actions['export_as_csv'] = (self.export_as_csv, 'export_as_csv', "Export selected %(verbose_name_plural)s as CSV")
return actions
So here, we have included the custom fields and the corresponding values for it. You can feel free to edit the fields and insert values as needed.
Now, if you click on export as csv, it would generate a result like this.
Workshop | Venue | Student Name | Student Email | Student Contact |
---|---|---|---|---|
Cyber Secuirty Fundamentals | Sapphire Auditorium | Mustafa | mustafa@edkool.com | 1234567890 |
Block Chain Technology | Main Seminar Hall | Mustafa | mustafa@edkool.com | 1234567890 |
So this way, custom fields as well can be added inside the export action.