|
3 | 3 | from .models import Question, QuizProgress, Topic, UserAnswer |
4 | 4 |
|
5 | 5 |
|
| 6 | +# Inline for Questions |
| 7 | +class QuestionInline(admin.TabularInline): |
| 8 | + model = Question |
| 9 | + extra = 1 # Number of empty forms to display |
| 10 | + |
| 11 | + |
6 | 12 | # Customize admin interface for Topic |
7 | 13 | class TopicAdmin(admin.ModelAdmin): |
8 | | - list_display = ("id", "name") # Display the ID and name of topics |
9 | | - search_fields = ("name",) # Enable search by name |
| 14 | + list_display = ("id", "name", "difficulty") |
| 15 | + search_fields = ("name",) |
| 16 | + list_filter = ("difficulty",) |
| 17 | + inlines = [QuestionInline] |
10 | 18 |
|
11 | 19 |
|
12 | 20 | # Customize admin interface for Question |
13 | 21 | class QuestionAdmin(admin.ModelAdmin): |
14 | | - list_display = ("id", "topic", "text", "correct_answer") # Show key details |
15 | | - list_filter = ("topic",) # Add a filter by topic |
16 | | - search_fields = ("text",) # Enable search by question text |
| 22 | + list_display = ("id", "topic", "text", "correct_answer") |
| 23 | + list_filter = ("topic", "topic__difficulty") |
| 24 | + search_fields = ("text",) |
| 25 | + fieldsets = ( |
| 26 | + (None, {"fields": ("topic", "text")}), |
| 27 | + ("Options", {"fields": ("option_a", "option_b", "option_c", "option_d")}), |
| 28 | + ("Correct Answer", {"fields": ("correct_answer",)}), |
| 29 | + ) |
| 30 | + radio_fields = { |
| 31 | + "correct_answer": admin.HORIZONTAL |
| 32 | + } # Use radio buttons for correct_answer |
17 | 33 |
|
18 | 34 |
|
19 | 35 | # Customize admin interface for UserAnswer |
20 | 36 | class UserAnswerAdmin(admin.ModelAdmin): |
21 | | - list_display = ( |
22 | | - "id", |
23 | | - "user", |
24 | | - "question", |
25 | | - "selected_answer", |
26 | | - "is_correct", |
27 | | - ) # Show key details |
28 | | - list_filter = ("is_correct",) # Filter by correctness |
29 | | - search_fields = ( |
30 | | - "user__username", |
31 | | - "question__text", |
32 | | - ) # Enable search by user or question |
| 37 | + list_display = ("id", "user", "question", "selected_answer", "is_correct") |
| 38 | + list_filter = ("is_correct",) |
| 39 | + search_fields = ("user__username", "question__text") |
| 40 | + readonly_fields = ("is_correct",) # Make is_correct read-only |
33 | 41 |
|
34 | 42 |
|
35 | 43 | # Customize admin interface for QuizProgress |
36 | 44 | class QuizProgressAdmin(admin.ModelAdmin): |
37 | | - list_display = ( |
38 | | - "id", |
39 | | - "user", |
40 | | - "topic", |
41 | | - "current_question", |
42 | | - "completed", |
43 | | - ) # Show progress details |
44 | | - list_filter = ("completed",) # Filter by completion status |
45 | | - search_fields = ("user__username", "topic__name") # Enable search by user or topic |
| 45 | + list_display = ("id", "user", "topic", "current_question", "completed") |
| 46 | + list_filter = ("completed",) |
| 47 | + search_fields = ("user__username", "topic__name") |
| 48 | + actions = ["mark_as_completed"] |
| 49 | + |
| 50 | + def mark_as_completed(self, request, queryset): |
| 51 | + queryset.update(completed=True) |
| 52 | + |
| 53 | + mark_as_completed.short_description = "Mark selected progress records as completed" |
46 | 54 |
|
47 | 55 |
|
48 | 56 | # Register models with their respective ModelAdmin |
|
0 commit comments