Django Admin Interface
Relevant source files
The following files were used as context for generating this wiki page:The Django Admin Interface in the Campuspal Room Finder project serves as the primary back-office tool for managing the core data models: Room and ClassSchedule. It is configured to provide a user-friendly and efficient experience for administrators, featuring customized list views, powerful filtering and search capabilities, and optimized database queries. This interface is essential for populating and maintaining the room and schedule data that the main application relies on.
The admin interface is enabled through the standard Django administrative command-line utility and application configuration. While the main user-facing application has a separate data upload feature for staff members, the Django Admin provides a more granular, direct way to view, create, edit, and delete records.
Sources: finder/admin.py, roomfinder/settings.py:32, finder/views.py:113
Setup and Configuration
The Django Admin is a built-in Django application that is enabled and configured within the project's settings.
Application Installation
The admin interface is activated by including "django.contrib.admin" in the INSTALLED_APPS list in the project's settings file.
Sources: roomfinder/settings.py:32
Custom Site Titles
The admin site's header, title, and index title have been customized to reflect the "Campuspal Room Finder" branding, making the interface more intuitive for administrators.
| Setting | Value |
|---|---|
site_header | "Campuspal Room Finder Admin" |
site_title | "Room Finder Admin" |
index_title | "Welcome to Room Finder Administration" |
Sources: finder/admin.py:70-72
Data Models and Admin Registration
The admin interface is configured to manage the Room and ClassSchedule models. The relationship between these models is central to the application's functionality.
This diagram shows the one-to-many relationship where one Room can have multiple ClassSchedule entries.
The following sections detail how each model is represented and managed in the admin interface.
Room Administration (RoomAdmin)
The RoomAdmin class customizes the display and functionality for the Room model in the admin interface.
# finder/admin.py:6-16
@admin.register(Room)
class RoomAdmin(admin.ModelAdmin):
list_display = ['name', 'building', 'get_schedule_count']
list_filter = ['building']
search_fields = ['name', 'building']
ordering = ['building', 'name']
def get_schedule_count(self, obj):
return obj.schedules.count()
get_schedule_count.short_description = 'Scheduled Classes'
Configuration
| Option | Value | Description |
|---|---|---|
list_display | ['name', 'building', 'get_schedule_count'] | Columns shown in the room list view. Includes a custom method to show the number of scheduled classes. |
list_filter | ['building'] | Adds a filter sidebar to narrow down rooms by their building. |
search_fields | ['name', 'building'] | Enables a search bar to find rooms by their name or building. |
ordering | ['building', 'name'] | Sets the default sorting order for the room list, first by building, then by name. |
get_schedule_count | Custom method | A calculated field that displays the total count of ClassSchedule objects related to each room. |
Sources: finder/admin.py:8-11, finder/admin.py:13-15
Class Schedule Administration (ClassScheduleAdmin)
The ClassScheduleAdmin class provides extensive customization for managing ClassSchedule entries, focusing on usability and performance.
# finder/admin.py:18-35
@admin.register(ClassSchedule)
class ClassScheduleAdmin(admin.ModelAdmin):
list_display = [
'course', 'subject', 'day', 'start_time', 'end_time',
'room', 'lecturer', 'is_cancelled'
]
list_filter = [
'day', 'is_cancelled', 'room__building',
'start_time', 'end_time'
]
search_fields = [
'course', 'subject', 'lecturer',
'room__name', 'room__building'
]
list_editable = ['is_cancelled']
ordering = ['day', 'start_time', 'room']
Configuration
| Option | Value | Description |
|---|---|---|
list_display | ['course', 'subject', 'day', 'start_time', 'end_time', 'room', 'lecturer', 'is_cancelled'] | Defines the columns visible in the schedule list view for a comprehensive overview. |
list_filter | ['day', 'is_cancelled', 'room__building', 'start_time', 'end_time'] | Provides multiple filters, including filtering by the building of the related room (room__building). |
search_fields | ['course', 'subject', 'lecturer', 'room__name', 'room__building'] | Allows searching across schedule fields and related Room fields (name and building). |
list_editable | ['is_cancelled'] | Allows administrators to toggle the is_cancelled status directly from the list view, without needing to open the detail page. |
ordering | ['day', 'start_time', 'room'] | Sorts schedules chronologically by day and start time. |
fieldsets | (('Course Information', ...), ('Schedule Details', ...), ('Status', ...)) | Organizes the fields on the add/edit page into logical groups: 'Course Information', 'Schedule Details', and 'Status', improving readability. |
get_queryset | Custom method with select_related('room') | Optimizes database performance by pre-fetching the related Room object in a single query, avoiding numerous extra queries when displaying the list of schedules. |
Sources: finder/admin.py:20-35, finder/admin.py:38-50, finder/admin.py:52-54
Query Optimization
The get_queryset method is overridden to improve performance. The flow diagram below illustrates how select_related reduces database hits.
This diagram shows the optimized query flow in ClassScheduleAdmin.
Sources: finder/admin.py:52-54
Summary
The Django Admin for the Campuspal Room Finder is a well-configured and essential component for data management. Through custom ModelAdmin classes, it provides administrators with powerful tools for viewing and editing Room and ClassSchedule data. Key features include tailored list displays, robust search and filtering, in-line editing, and performance optimizations like select_related. These customizations ensure that the administrative backend is not only functional but also efficient and user-friendly.