[Django]-How to show the correct name object in Django admin instead 'XXX object'

7👍

✅

This part of the form deals with editing the ManyToManyField. It will use the __str__ of the model that is referenced.

In order to thus change the textual representation, you need to implement the __str__ of the Course model, like:

class Course(models.Model):  # Course model, not Person
    name = models.CharField(max_length=128)

    def __str__(self):
        return self.name

Setting the __str__ of a person to self.course.name does not seem correct, since you probably do not want to represent a Person by the name of its courses. Furthermore since this is a ManyToManyField, it will not work anyway.

Note: usually the name of ManyToManyFields is plural, so courses, instead of course, since it is basically a collection of Courses, not a single Course.

Leave a comment