0

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

manosim
  • 3,630
  • 12
  • 45
  • 68

1 Answers1

1

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.

Kinsa
  • 686
  • 8
  • 14
  • Perfect! That was really fast! So with this `__unicode__(self)` every time that a **course** will be return it will return also it's type of attendance? Correct? – manosim Mar 27 '14 at 20:40
  • Yes. You could create a custom method within the `Course` model to return the name plus attendance if that's a negative as that's the primary advantage to assigning that value to the `__unicode__()` method. – Kinsa Mar 27 '14 at 20:42
  • Exactly what I was looking for! So in my admin.py how will I ask for `course_and_attendance`? – manosim Mar 27 '14 at 20:48
  • I think this will help you: http://stackoverflow.com/questions/6836740/django-admin-change-foreignkey-display-text – Kinsa Mar 27 '14 at 20:58
  • I'll check it!! Once again, thank you for your help! – manosim Mar 27 '14 at 21:15