2

I have a many to many field, which I'm displaying in the django admin panel. When I add multiple items, they all come up as "ASGGroup object" in the display selector. Instead, I want them to come up as whatever the ASGGroup.name field is set to. How do I do this?

My models looks like:

class Thing(Model):
    read_groups = ManyToManyField('ASGGroup', related_name="thing_read", blank=True)

class ASGGroup(Model):
    name = CharField(max_length=63, null=True)

But what I'm seeing the m2m widget display is:

m2m display

Adam Morris
  • 8,265
  • 12
  • 45
  • 68

1 Answers1

3

You need to define the __unicode__ (or __str__ if you are using Python 3) method on your models, so:

class ASGGroup(Model):
    name = CharField(max_length=63, null=True)

    def __unicode__(self):
        return self.name

Now when your model is parsed as a string, it will return the model's name field, rather than the class name.

Johndt
  • 4,187
  • 1
  • 23
  • 29