Django Admin更改下拉标签

时间:2014-09-03 15:48:09

标签: django admin

我想知道如何更改Django Admin创建的下拉列表的标签。 下面是两个模型:

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'))

我需要的是:

添加新媒体时选择比赛的下拉菜单不仅应显示比赛名称,还应显示比赛date_added。

非常感谢任何帮助或指示。

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以通过创建自己的管理表单来实现此目的,该管理表单使用ModelChoiceField的子类race,覆盖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)

然后,在您的管理员中,将MediaAdminForm设置为您的管理类的form值。

您可以使用管理员中的formfield_for_foreignkey挂钩返回RaceChoiceField而不是创建一个全新的表单,但是我必须阅读源代码以确保我获得了详细信息我不会写那个版本。

这将影响更改视图以及添加视图 - 如果要将其限制为仅添加视图,则会变得更难。您必须覆盖管理员的add_view方法。