Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the FinalGradeAdmin to display specific fields from the related course_run model. However, the use of double-underscore syntax in list_display is not supported by Django and will cause a SystemCheckError. It is recommended to use custom methods to access these related fields and to include list_select_related to prevent N+1 query performance issues.
| """Admin for FinalGrade""" | ||
| model = models.FinalGrade | ||
| list_display = ('id', 'grade', 'user', 'course_run', ) | ||
| list_display = ('id', 'grade', 'user', 'course_run__edx_course_key', 'course_run__title') |
There was a problem hiding this comment.
Django's list_display does not support the double-underscore (__) syntax for traversing relationships directly. Using course_run__edx_course_key and course_run__title will result in a SystemCheckError (FieldDoesNotExist). To display fields from a related model, you should define methods on the ModelAdmin class.
Additionally, you should use list_select_related to avoid N+1 query performance issues when displaying these related fields.
list_display = ('id', 'grade', 'user', 'get_edx_course_key', 'get_course_run_title')
list_select_related = ('course_run', 'user')
def get_edx_course_key(self, obj):
return obj.course_run.edx_course_key
get_edx_course_key.short_description = 'Edx Course Key'
get_edx_course_key.admin_order_field = 'course_run__edx_course_key'
def get_course_run_title(self, obj):
return obj.course_run.title
get_course_run_title.short_description = 'Course Run Title'
get_course_run_title.admin_order_field = 'course_run__title'af9128b to
30c1361
Compare
What are the relevant tickets?
N/A
Description (What does it do?)
List Final Grades with edx_course_key
How can this be tested?
Go to /admin/