Adam Morris
Adam Morris

Reputation: 8545

Django: name of many to many items in the admin interface

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

Upvotes: 2

Views: 82

Answers (1)

Johndt
Johndt

Reputation: 4337

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.

Upvotes: 3

Related Questions