python - Django 模型表单集 - 修改表单标签和默认值

标签 python django django-forms

我正在构建一个这样的表单集:

InterestFormSet = modelformset_factory(Interest, \
    formset=BaseInterestFormSet, exclude=('userid',), extra=2) 

我想为此表单的元素设置默认标签和值。

我知道在简单的表单中,我可以使用 fields 字典来更改表单的特定字段的这些内容,但是如何使用表单集来完成此操作?

我尝试扩展表单集(如您所见),看看是否可以从 __init__ 内访问 self.fields,但没有成功。

最佳答案

像这样的事情应该做你想要的:

class InterestForm(ModelForm):
    pub_date = DateField(label='Publication date')

    class Meta:
        model = Interest
        exclude = ('userid',)


InterestFormSet = modelformset_factory(Interest, form=InterestForm, extra=2)

关于python - Django 模型表单集 - 修改表单标签和默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4115541/

相关文章:

python - Django 聚合,计数总和

python - Django REST Framework 序列化程序返回对象而不是数据

python - 仅在未运行时使用 cron 运行 python 脚本

python - Python 3.2 中的 AttributeError 跨 TypeError 问题

python - 无法查看 aws s3 上的文件(403 禁止)

python - Django 1.9 FormView永远无法到达get_context_data

python - django - 将用户 ID 添加到 UpdateView

python - 不允许同时指定 'fields' 和 'form_class'

python - 元组列表的唯一组合

python - close() 在 matplotlib savefig 之后不释放内存