admin.py 1.39 KB
Newer Older
1 2
# standard library imports
from __future__ import absolute_import, print_function
3
# core django imports
Jason D Yeomans's avatar
Jason D Yeomans committed
4
from django.contrib import admin
5
# imports from your apps
6
from .models import Student, Major, Confirmation
Jason D Yeomans's avatar
Jason D Yeomans committed
7

Daniel W Bond's avatar
pep8 me  
Daniel W Bond committed
8

9
class StudentAdmin(admin.ModelAdmin):
10
    list_display = ("get_name", "room", "privacy", "get_first_major", "created")
11 12 13

    def get_name(self, student):
        return student.get_full_name_or_uname()
14

15 16 17
    get_name.short_description = 'Name'
    get_name.admin_order_field = 'user__username'  # ordering by callables is hard

18 19 20 21 22 23 24 25
    # We cannot use a manytomanyfield as a field in the list, so we're getting the first
    # major. If we need to see a student's second major, we can just click the student.
    # This covers nearly all students, who will have only one major.
    def get_first_major(self, student):
        return student.major.first()

    get_first_major.short_description = 'Major'
    # we're not going to give the option to sort by major for now
26

Daniel W Bond's avatar
Daniel W Bond committed
27

28 29 30 31 32 33 34 35 36 37 38
class MajorAdmin(admin.ModelAdmin):
    list_display = ("name", "get_major_num", )

    def get_major_num(self, major):
        student_num = Student.objects.filter(major=major).count()
        return student_num
    get_major_num.short_description = 'Number of Students'
    # ordering is hard (ditto above; rewrite queryset :-/)

admin.site.register(Student, StudentAdmin)
admin.site.register(Major, MajorAdmin)
39
admin.site.register(Confirmation)