2014-03-31 124 views
0

請幫助創建selectBox adminpaneli。我使用: models.py:如何創建選擇框?

from django.db import models 
from django.forms.models import ChoiceField 
from django import forms 

CHOICES = ('volvo', 'saab', 'opel', 'audi', 'cyan',) 


class Sponsors(models.Model): 
    title = models.CharField(max_length=30) 
    link = models.URLField(max_length=100) 
    background_color = forms.ChoiceField(widget=forms.Select, choices=CHOICES) 

admin.py:

from django.contrib import admin 
from sponsors.models import Sponsors 


class sponsorsAdmin(admin.ModelAdmin): 
    fields = ['title', 'link', 'background_color'] 
    list_display = ('title',) 
    search_fields = ['title'] 

    class Meta: 
     verbose_name = 'Спонсор' 
     verbose_name_plural = 'Спонсоры'   


admin.site.register(Sponsors, sponsorsAdmin) 

其結果是,管理面板纔可見域 「ID」, 「標題」, 「鏈接」。但是「background_color」字段不可見。它在數據庫中也不存在。

我需要adminpanel還田「BACKGROUND_COLOR」看起來像this

回答

0

而不是增加了background_color領域模型,你應該繼承的ModelForm包括該字段。

class Sponsors(models.Model): 
    title = models.CharField(max_length=30) 
    link = models.URLField(max_length=100) 


CHOICES = ('volvo', 'saab', 'opel', 'audi', 'cyan',) 


class SponsorsForm(models.ModelForm): 

    class Meta: 
     model = Sponsors 

    background_color = forms.ChoiceField(choices=CHOICES) 


class sponsorsAdmin(admin.ModelAdmin): 
    ... 
    form = SponsorsForm 
相關問題