2014-09-03 204 views
0

我想知道如何更改由Django Admin創建的下拉列表的標籤。 以下兩個型號:Django管理員更改下拉標籤

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以及比賽。

任何幫助或方向將不勝感激。

謝謝!

+0

無法在Race模型上定義__str__/__unicode__? – 2014-09-03 15:57:31

+0

路易斯,它被定義,但我需要它只是賽名。我唯一需要的名稱和時間戳是在頁面上添加新媒體。 – 2014-09-03 17:44:12

回答

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方法。

+0

非常感謝彼得! – 2014-09-03 18:57:31