from django.contrib import admin from vbv_lernwelt.course.models import CourseSessionUser from vbv_lernwelt.learning_mentor.models import LearningMentor, MentorInvitation @admin.register(LearningMentor) class LearningMentorAdmin(admin.ModelAdmin): def participant_count(self, obj): return obj.participants.count() participant_count.short_description = "Participants" list_display = ["mentor", "course_session", "participant_count"] search_fields = ["mentor__email"] raw_id_fields = [ "mentor", "course_session", ] def formfield_for_manytomany(self, db_field, request, **kwargs): if db_field.name == "participants": if request.resolver_match.kwargs.get("object_id"): object_id = str(request.resolver_match.kwargs.get("object_id")) lm = LearningMentor.objects.get(id=object_id) kwargs["queryset"] = CourseSessionUser.objects.filter( course_session=lm.course_session ) return super().formfield_for_manytomany(db_field, request, **kwargs) @admin.register(MentorInvitation) class MentorInvitationAdmin(admin.ModelAdmin): list_display = ["id", "email", "participant", "created"] readonly_fields = ["id", "created"] search_fields = ["email"]