Skip to content

Question admin

question.admin:

QuestionGroupAdmin

Bases: ModelAdmin

Source code in question/admin.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class QuestionGroupAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.ManyToManyField: {'widget': CheckboxSelectMultiple},
    }

    def get_form(self, request, obj=None, **kwargs):
        """This method is needed because setting the QuestionGroup.questions field as readonly
            for built-in (i.e., not editable) groups shows the questions as one-line of concatenated strings, which is ugly.
            Instead, this method allows to keep the checkboxes, but disabled"""
        form = super().get_form(request, obj, **kwargs)

        if obj and not obj.editable:
            for field_name in form.base_fields:
                form.base_fields[field_name].disabled = True

        return form

get_form(request, obj=None, **kwargs)

This method is needed because setting the QuestionGroup.questions field as readonly for built-in (i.e., not editable) groups shows the questions as one-line of concatenated strings, which is ugly. Instead, this method allows to keep the checkboxes, but disabled

Source code in question/admin.py
69
70
71
72
73
74
75
76
77
78
79
def get_form(self, request, obj=None, **kwargs):
    """This method is needed because setting the QuestionGroup.questions field as readonly
        for built-in (i.e., not editable) groups shows the questions as one-line of concatenated strings, which is ugly.
        Instead, this method allows to keep the checkboxes, but disabled"""
    form = super().get_form(request, obj, **kwargs)

    if obj and not obj.editable:
        for field_name in form.base_fields:
            form.base_fields[field_name].disabled = True

    return form