我有如下看三個模型類:與下拉選擇字段替換文本框
class Model(models.Model):
model = models.CharField(max_length=20, blank=False)
manufacturer = models.ForeignKey(Manufacturer)
date_added = models.DateField(default=datetime.today)
def __unicode__(self):
name = ''+str(self.manufacturer)+" "+str(self.model)
return name
class Series(models.Model):
series = models.CharField(max_length=20, blank=True, null=True)
model = models.ForeignKey(Model)
date_added = models.DateField(default=datetime.today)
def __unicode__(self):
name = str(self.model)+" "+str(self.series)
return name
class Manufacturer(models.Model):
MANUFACTURER_POPULARITY_CHOICES = (
('1', 'Primary'),
('2', 'Secondary'),
('3', 'Tertiary'),
)
manufacturer = models.CharField(max_length=15, blank=False)
date_added = models.DateField(default=datetime.today)
manufacturer_popularity = models.CharField(max_length=1,
choices=MANUFACTURER_POPULARITY_CHOICES)
def __unicode__(self):
return self.manufacturer
我想有字段model
series
並表示爲下拉列表,而不是文本字段manufacturer
。我已經定製了模型的形式如下:
class SeriesForm(ModelForm):
series = forms.ModelChoiceField(queryset=Series.objects.all())
class Meta:
model = Series
exclude = ('model', 'date_added',)
class ModelForm(ModelForm):
model = forms.ModelChoiceField(queryset=Model.objects.all())
class Meta:
model = Model
exclude = ('manufacturer', 'date_added',)
class ManufacturerForm(ModelForm):
manufacturer = forms.ModelChoiceField(queryset=Manufacturer.objects.all())
class Meta:
model = Manufacturer
exclude = ('date_added',)
然而,下拉菜單被填充在各自的類unicode的...我怎麼能進一步自定義此得到最終的結果我想要什麼?
另外,我怎樣才能填寫正確的數據編輯窗體?目前只有SeriesForm
被填充。所有這一切的出發點是從另一個類,它的聲明如下:
class CommonVehicle(models.Model):
year = models.ForeignKey(Year)
series = models.ForeignKey(Series)
....
def __unicode__(self):
name = ''+str(self.year)+" "+str(self.series)
return name
這正是我需要的....讓我試試吧。日Thnx – Stephen 2010-03-22 08:23:54