[Django]-How to customize the label displayed for a ManyToManyField in an inline model admin?

3👍

An quicker and dirtier alternative to defining a model class for the relationship is to redefine the __str__ or __unicode__ method:

class ToppingInline(admin.TabularInline):
    ...
    Pizza.toppings.through.__str__ = lambda self: 'Topping'

2👍

You would control what displays for an object using the __str__ or __unicode__ method.

You could manage the many to many yourself to define this attribute like this:

...

class Pizza(models.Model):
    toppings = models.ManyToManyField(Topping, through="PizzaToppingRelationship")


class PizzaToppingRelationship(models.Model):
    pizza = models.ForeignKey(Pizza)
    topping = models.ForeignKey(Topping)

    def __str__(self):
        return 'Pizza Topping - {}'.format(self.topping.name)

Leave a comment