0

I'm writing a django program for a school. I have a student model, a schoolclass model, and an enrolment model. The schoolclass has a ManyToMany relationship with student through the enrolment model. I have a TabularInline in the admin view for a student which shows which classes that student is enrolled in (and similarly, in a schoolclass, I can see which students are enrolled in the class). However, when I look at a student in admin, and see all of the enrolled classes, each class is shown as a drop-down of all possible classes (of which there are a large number), and similarly the schoolclass admin view shows all students in a dropdown for each enrolment.

Is it possible to merely show the enrolment in the inline, rather than be able to change it in the dropdown? I would still like to be able to delete it, and add new enrolments at the bottom, but not change existing enrolments. It would also be nice to have a link to the related object (e.g. click on a schoolclass when in student view takes me to that schoolclass info)

askvictor
  • 3,621
  • 4
  • 32
  • 45
  • duplicate of http://stackoverflow.com/questions/3967644/django-admin-how-to-display-a-field-that-is-marked-as-editable-false-in-the-mo – Colleen Jun 07 '13 at 03:39

1 Answers1

0

I ended up using the following for my inline; the both gets rid of the dropdown for each student (containing all the students, which slows things down considerably), and adds a link to the admin for each student. I've got very similar code for the reverse relationship.

class SchoolClassStudentsInline(admin.TabularInline):
    model = Enrolment

    def student_link(self, instance):
        url = reverse('admin:%s_%s_change' % (
            instance._meta.app_label,  instance.student._meta.module_name),  args=[instance.student.id] )
        return mark_safe(u'<a href="{u}">{s}</a>'.format(u=url,s=instance.student))

    fields = ('student_link',)
    readonly_fields = ('student_link',)
    extra = 0
askvictor
  • 3,621
  • 4
  • 32
  • 45