Thiago Mambretti
Thiago Mambretti

Reputation: 70

Django Admin change drop-down label

I would like to know how can I change the label of a drop-down created by Django Admin. Below the two models:

class Media(models.Model):
    race = models.ForeignKey(Race, verbose_name = _('race'), blank=True, null=True)
    caption = models.CharField(verbose_name=_('caption'), max_length=255, blank=True, null=True)
    credit = models.CharField(verbose_name=_('credit'), max_length=255, blank=True, null=True)
    image = FileBrowseField("Image", max_length=200)

    # Meta
    date_updated = models.DateTimeField(auto_now=True, auto_now_add=True, verbose_name=_('date updated'))
    date_added = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name=_('date added'))

class Race(models.Model):
    category = models.ForeignKey(Category, verbose_name = _('category'))
    name = models.CharField(max_length=255, verbose_name=_('name'))
    slug = models.SlugField(max_length = 128, help_text = _('Automatic generated. Do not edit!'))
    state = models.CharField(max_length=255, verbose_name=_('state'), blank=True, null=True)
    city = models.CharField(max_length=255, verbose_name=_('city'), blank=True, null=True)
    description = models.TextField(verbose_name=_('description'), blank=True, null=True)
    banner = models.FileField(max_length = 255, upload_to = '_static/img/races/', verbose_name = _('banner'), blank=True, null=True)
    title = models.FileField(max_length = 255, upload_to = '_static/img/races/', verbose_name = _('title'), blank=True, null=True)
    bigbox = models.FileField(max_length = 255, upload_to = '_static/img/races/', verbose_name = _('big box'))
    midbox = models.FileField(max_length = 255, upload_to = '_static/img/races/', verbose_name = _('medium box'), blank=True, null=True)
    smallbox = models.FileField(max_length = 255, upload_to = '_static/img/races/', verbose_name = _('small box'), blank=True, null=True)
    course = models.FileField(max_length = 255, upload_to = '_static/img/races/', verbose_name = _('course'), blank=True, null=True)
    rules = models.CharField(max_length=255, blank=True, verbose_name=_('rules'))
    results = models.CharField(max_length=255, blank=True, verbose_name=_('results'))
    date = models.DateTimeField(auto_now=False, auto_now_add=False, verbose_name=_('date'), blank=True, null=True)
    active = models.BooleanField(default=True)

    # Meta
    date_updated = models.DateTimeField(auto_now=True, auto_now_add=True, verbose_name=_('date updated'))
    date_added = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name=_('date added'))

What I need is this:

The drop-down to select a race when adding a new media should show not only the race name but the race date_added as well.

Any help or direction will be much appreciated.

Thanks!

Upvotes: 1

Views: 1457

Answers (1)

Peter DeGlopper
Peter DeGlopper

Reputation: 37319

You can do this by creating your own admin form that uses a subclass of ModelChoiceField for race, one that overrides label_from_instance. EG:

from django.forms import ModelChoiceField, ModelForm

class RaceChoiceField(ModelChoiceField):
    def label_from_instance(self, race_instance):
        return race.name + ' ' + str(race.date)

class MediaAdminForm(ModelForm):
    class Meta:
        model = Media
    race = RaceChoiceField(Race)

Then, in your admin, set MediaAdminForm as the form value for your admin class.

You could probably use the formfield_for_foreignkey hook in the admin to return a RaceChoiceField rather than create a whole new form, but I'd have to read the source code to make sure I got the details exact so I'm not going to write up that version.

This will affect the change view as well as the add view - if you want to limit it to just the add view it gets harder. You'd have to override the add_view method on your admin.

Upvotes: 1

Related Questions