免责声明:我是Python和Django的初学者,但有Drupal编程经验.
如何覆盖以下内容的默认小部件:
#models.py
class Project(models.Model):
color_mode = models.CharField(max_length=50, null=True, blank=True, help_text='colors - e.g black and white, grayscale')
在我的表单中使用 Select 框吗?以下内容可以吗?还是我漏掉了什么?
#forms.py
from django.forms import ModelForm, Select
class ProjectForm(ModelForm):
class Meta:
model = Project
fields = ('title', 'date_created', 'path', 'color_mode')
colors = (
('mixed', 'Mixed (i.e. some color or grayscale, some black and white)'),
('color_grayscale', 'Color / Grayscale'),
('black_and_white', 'Black and White only'),
)
widgets = {'color_mode': Select(choices=colors)}
读完https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets之后,我不知所措,因为示例中只讨论了TextArea,而小部件讨论似乎排除了ModelForm.
谢谢!