def build_enum_field(self, attr, **kwargs):
        """
        Build field for sqlalchemy enum type.
        """
        kwargs.pop("max_length", None)

        if attr.column.type.enum_class:
            kwargs["enum_class"] = attr.column.type.enum_class
            return EnumField(**kwargs)

        kwargs["choices"] = [(val, val) for val in attr.column.type.enums]
        return djangofields.TypedChoiceField(**kwargs)
Exemple #2
0
class TestForm(forms.Form):
    user = fields.CharField(
        required=True,
        max_length=12,
        min_length=3,
        error_messages={},
        widget=widgets.TextInput(attrs={'class': 'c1'}),  # 定制HTML插件
        label='用户名',
        initial='请输入用户名',
        help_text='帮助信息',
        show_hidden_initial=True,  # 插件后面加一个隐藏且具有默认值的插件(可用于检验两次输入是否一致)
        validators=[],  # 自定义验证规则
        localize=True,  # 是否支持本地化
        disabled=True,  # 是否不能编辑
        label_suffix=':',
    )
    age = fields.IntegerField()

    email = fields.EmailField()

    img = fields.FileField()

    city = fields.ChoiceField(
        choices=[
            (1, '北京'),
            (2, '上海'),
            (3, '沙河'),
        ],
        initial=2,
    )
    city = fields.TypedChoiceField(
        coerce=lambda x: int(x),  # 类型转换 结果返回整型
        choices=[
            (1, '北京'),
            (2, '上海'),
            (3, '沙河'),
        ],
        initial=2,
    )

    hobby = fields.MultipleChoiceField(
        choices=[
            (1, 'a'),
            (2, 'b'),
            (3, 'c'),
        ],
        initial=[
            1,
            2,
        ],
    )
class TestForm(forms.Form):
    user = fields.CharField(
        required=True, # 是否必填
        max_length=12, # 最大长度
        min_length=3,  # 最小长度
        error_messages={}, # 错误提示
        widget = widgets.TextInput(attrs={'class':'c1'}), # 定制HTML插件
        label='用户名',
        initial='请输入用户',
        help_text='asdfasdf',
        show_hidden_initial=False,
        # validators=[]
        disabled=True,
        label_suffix='->'
    )
    age = fields.IntegerField(
        label='年龄',
        max_value= 12,
        min_value=5,
        error_messages={
            'max_value':'太大了'
        }
    )

    email = fields.EmailField(
        label='邮箱'
    )

    img = fields.FileField()

    city = fields.TypedChoiceField(
        coerce=lambda x: int(x),
        choices=[(1,'上海',),(2,'北京'),(3,'沙河'),],
        initial=2
    )
    bobby = fields.MultipleChoiceField(
        choices=[(1,'刚娘'),(2,'铁娘'),(3,'钢弹')],
        initial=[1,2]
    )

    xoo = fields.FilePathField(
        path='app01'
    )
Exemple #4
0
class FieldForm(DForms.Form):
    f1 = fields.CharField(max_length=6,
                          required=True,
                          initial="小虎",
                          validators=[
                              RegexValidator(r'^[0-9]+$', '11111', code='f1'),
                              RegexValidator(r'^159[0-9]+$', '2222', code='f2')
                          ],
                          error_messages={
                              'required': '不能为空',
                              'f1': 'geshicuowu',
                              'f2': 'kajdlfkjasldf',
                              'max_length': 'taichangla'
                          },
                          label='爱抚1',
                          show_hidden_initial=True,
                          disabled=False,
                          label_suffix="-->")
    # f2 = fields.RegexField(r'^159[0-9]+$')
    f3 = fields.FileField()

    f4 = fields.ChoiceField(initial=2,
                            choices=[(
                                1,
                                '赵四',
                            ), (2, "刘能"), (3, '六大脑袋')])
    f5 = fields.TypedChoiceField(coerce=lambda x: int(x),
                                 initial=2,
                                 choices=[(
                                     1,
                                     '赵四',
                                 ), (2, "刘能"), (3, '六大脑袋')])

    f6 = fields.MultipleChoiceField(initial=[1, 2],
                                    choices=[(
                                        1,
                                        '赵四',
                                    ), (2, "刘能"), (3, '六大脑袋')])
    f7 = fields.SplitDateTimeField()
    f8 = fields.FilePathField(path='app01', allow_folders=True, recursive=True)
Exemple #5
0
    def __init__(self, *args, **kwargs):
        super(AnswerForm, self).__init__(*args, **kwargs)

        if self.instance.metric.type == 'yesno':
            self.fields = OrderedDict([
                ('answer',
                 fields.TypedChoiceField(
                     choices=[
                         ('1.0', 'Yes'),
                         ('0.0', 'No'),
                     ],
                     coerce=lambda v: float(v),
                     empty_value='',
                     widget=forms.RadioSelect(),
                     required=True,
                 )),
            ])
        elif self.instance.metric.type == 'yesnobut':
            self.fields = OrderedDict([
                ('answer',
                 fields.TypedChoiceField(
                     choices=[
                         ('1.0', 'Yes'),
                         ('0.75', 'Yes, but'),
                         ('0.25', 'No, but'),
                         ('0.0', 'No'),
                     ],
                     coerce=lambda v: float('nan') if v == '' else float(v),
                     empty_value='',
                     widget=forms.RadioSelect(),
                     required=True,
                 )),
                ('url_comment',
                 forms.CharField(
                     widget=forms.Textarea(attrs={
                         'placeholder':
                         'Enter URLs, if applicable and available. Separate URLs by spaces or new lines.',
                         'rows': '2',
                     }, ),
                     required=False,
                 )),
                ('comment',
                 forms.CharField(
                     widget=forms.Textarea(attrs={
                         'placeholder':
                         'Please explain or describe your answer.',
                         'rows': '2',
                     }, ),
                     required=False,
                 )),
            ])
        elif self.instance.metric.type == 'yesnomaybe':
            self.fields = OrderedDict([
                ('answer',
                 fields.TypedChoiceField(
                     choices=[
                         ('1.0', 'Yes'),
                         ('0.5', 'Maybe'),
                         ('0.0', 'No'),
                     ],
                     coerce=lambda v: float(v),
                     empty_value='',
                     widget=forms.RadioSelect(),
                     required=True,
                 )),
            ])
        elif self.instance.metric.type == 'url':
            self.fields = OrderedDict([
                ('answer',
                 fields.TypedChoiceField(
                     choices=[
                         ('1.0', 'Yes'),
                         ('0.0', 'No'),
                     ],
                     coerce=lambda v: float(v),
                     empty_value='',
                     widget=forms.RadioSelect(),
                     required=True,
                 )),
                ('url_comment',
                 forms.CharField(
                     widget=forms.Textarea(attrs={
                         'placeholder':
                         'Enter URLs, if applicable and available. Separate URLs by spaces or new lines.',
                         'rows': '2',
                     }, ),
                     required=False,
                 )),
            ])
        elif self.instance.metric.type == 'text':
            self.fields = OrderedDict([
                ('answer',
                 fields.TypedChoiceField(
                     choices=[
                         ('1.0', 'Yes'),
                         ('0.0', 'No'),
                     ],
                     coerce=lambda v: float(v),
                     empty_value='',
                     widget=forms.RadioSelect(),
                     required=True,
                 )),
                ('comment',
                 forms.CharField(
                     widget=forms.Textarea(attrs={
                         'placeholder':
                         'Please explain or describe your answer.',
                         'rows': '2',
                     }, ),
                     required=False,
                 )),
            ])
        else:
            raise 'Type is invalid'
Exemple #6
0
class TestForm(forms.Form):
    user = fields.CharField(
        required=True,  # 是否为空
        max_length=12,  # 最大长度
        min_length=3,  # 最小长度
        error_messages={},
        # widget=widgets.Select(),     # 定制HTML插件
        label='用户名',
        # initial='用户名',
        # disabled=True,
        label_suffix=':',
    )
    age = fields.IntegerField(
        required=True,
        label='年龄',
        max_value=12,
        min_value=1,
    )
    email = fields.EmailField(required=True, label='Email')
    image = fields.FileField()
    city = fields.TypedChoiceField(
        coerce=lambda x: int(x),
        choices=[(
            1,
            '上海',
        ), (
            2,
            '北京',
        ), (
            3,
            '武汉',
        )],
        initial=2,
    )
    hobby = fields.MultipleChoiceField(choices=[(1, '篮球'), (2, '足球'),
                                                (3, '乒乓球')], )
    # xdd = fields.CharField(
    #     # widget=widgets.Select(choices=[(1, '张三',), (2, '李四',), (3, '王五',)])
    #     widget=widgets.CheckboxInput()
    # )
    xdd = fields.MultipleChoiceField(
        initial=[
            2,
        ],
        choices=((
            1,
            '上海',
        ), (
            2,
            '北京',
        ), (
            3,
            '天津',
        )),
        widget=widgets.CheckboxSelectMultiple,
    )
    # 单radio,值为字符串
    address = fields.CharField(
        initial=2,
        widget=widgets.RadioSelect(choices=(
            (1, '上海'),
            (2, '北京'),
        )),
    )
Exemple #7
0
class TestForm(forms.Form):
    user = fields.CharField(
        required=True,  #判断是否必填
        max_length=12,  #最大长度
        min_length=3,  #最下长度
        error_messages={},  #错误提示
        # widget = widgets.Select(),#widget可以定制HTML插件
        label='用户名',
        # initial='请输入用户名',#设置默认值
        disabled=True,
    )

    age = fields.IntegerField(label='年龄',
                              max_value=12,
                              min_value=5,
                              error_messages={'max_value': '输入的值超出限制'})

    email = fields.EmailField(label='邮箱', )

    #上传文件
    # 注:需要PIL模块,Pillow
    # 以上两个字典使用时,需要注意两点:
    # - form表单中
    # enctype = "multipart/form-data"
    # - view函数中
    # obj = MyForm(request.POST, request.FILES)

    img = fields.FileField(label='上传文件', )

    # city = fields.ChoiceField(
    #     label='城市',
    #     #数据源写在choices里面即可
    #     choices=[(1,'天津'),(2,'北京'),(3,'陕西')],
    #     #下拉框里面的默认值设定
    #     initial = 3,
    # )

    city = fields.TypedChoiceField(
        label='Typed城市',
        #接收一个参数,将该参数转换为int再返回
        coerce=lambda x: int(x),
        #数据源写在choices里面即可
        choices=[(1, '天津'), (2, '北京'), (3, '陕西')],
        #下拉框里面的默认值设定
        initial=3,
    )

    hobby = fields.MultipleChoiceField(
        label='爱好',
        choices=[(1, '爬山'), (2, '阅读'), (3, '健身')],
        #默认值多个,所以可以写列表
        initial=[1, 3],
    )

    #下拉单选框的两种写法
    selectA = fields.CharField(widget=widgets.Select(
        choices=[(1, 'winchoo'), (2, 'chason'), (3, '张三')]), )
    selectB = fields.ChoiceField(choices=[(1, 'winchoo'), (2, 'chason'),
                                          (3, '张三')], )

    #下拉多选框的写法
    Multiselect = fields.MultipleChoiceField(
        choices=[(1, 'winchoo'), (2, 'chason'), (3, '张三')],
        widget=widgets.SelectMultiple(attrs={'class': 'c1'}),  #这里的class是自定义属性
    )

    #单选框checkbox
    checkboxuser = fields.CharField(widget=widgets.CheckboxInput(), )

    #多选框checkbox
    Multicheckbox = fields.MultipleChoiceField(
        initial=[2, 'winchoo'],
        choices=((1, 'chason'), (2, 'winchoo'), (3, 'Milton')),
        widget=widgets.CheckboxSelectMultiple,
    )

    #单选RadioSelect
    Radioselect = fields.ChoiceField(
        choices=((1, '马来西亚'), (2, '美国'), (3, '日本')),
        initial=2,
        widget=widgets.RadioSelect,
    )