Skip to content

Commit 92d3e41

Browse files
admin panel now has the options to fully manage the the models data of quiz module
1 parent 6ab60c5 commit 92d3e41

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

Algolyzer/quiz/admin.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,54 @@
33
from .models import Question, QuizProgress, Topic, UserAnswer
44

55

6+
# Inline for Questions
7+
class QuestionInline(admin.TabularInline):
8+
model = Question
9+
extra = 1 # Number of empty forms to display
10+
11+
612
# Customize admin interface for Topic
713
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]
1018

1119

1220
# Customize admin interface for Question
1321
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
1733

1834

1935
# Customize admin interface for UserAnswer
2036
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
3341

3442

3543
# Customize admin interface for QuizProgress
3644
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"
4654

4755

4856
# Register models with their respective ModelAdmin

0 commit comments

Comments
 (0)