Question

I got a model where a "student" is able to enrol to a "course". The issue is that courses may have the same name(but different attendance, 'full time' or 'part time'). So as you can see in the image attached, I would like to know which course a student is enrolled to. Either inside the select box or next to it. I tried to use a list display but couldn't figure out how to use it.

Edit: the field that the type of attendance is stored is called 'attendance' and it's part of the class 'Course'.

admin.py

class StudentAdmin(admin.StackedInline):

    model = Student
    verbose_name_plural = 'students'

# Define a new User admin
class UserAdmin(UserAdmin):
    inlines = (StudentAdmin, )

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

1

Was it helpful?

Solution

On the Course model you can define the __unicode__(self): method to interpolate two attributes together. So, in your case you could combine the name of the course and the attendance.

That would look something like this:

class Course(models.Model):
    name = models.CharField(max_length=100)
    attendance = models.CharField(max_length=100)

    def __unicode__(self):
        return "%s (%s)" % (self.name, self.attendance)

Then the string representation of that course will be the name of the course followed by the attendance in parenthesis.

An example of this can be found in the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top