python - 是否可以动态分配字段类型 - Django Form?

标签 python django python-3.x django-forms

是否可以动态分配字段类型 - Django Form?

field_type = 'CharField'

field = forms.{field_type}(label='Field')

最佳答案

您可以使用getattr像这样

field_type = 'CharField'

field = getattr(forms, field_type)(label='Field')

只需确保 field_type 是有效的 Django 表单字段名称。

关于python - 是否可以动态分配字段类型 - Django Form?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55329655/

相关文章:

python - 新行字符导致文本清理期间单词分解

python - 从元组列表中删除部分重复的元组

python - 向 Django 添加自定义语言

python - 使用 "contextlib.nested"的 Python 代码在 Python 3 中应该是什么样子?

django - 如何将 serializers.RelatedField() 添加到 django rest 框架中的 Meta 类字段

python - 如何在 ajax 模板中为 Python 回调函数创建 Django 单击按钮

ruby-on-rails - AES-256-GCM Python 密码学与 Rails OpenSSL

python - 使用 Django 的 HMAC 身份验证 - 共享 key

python - Windows 错误 [5] : Access Denied H2o Deep Learning Framework Initialization using Python

python - 当 import_array 不在同一翻译单元中时出现段错误