模型中的动态表单要求 [英] dynamic form requirements from model

查看:110
本文介绍了模型中的动态表单要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据特定的客户需求生成动态注册表格。我创建了一个UserProfile模型,其中大多数字段设置为blank = True。

I'm trying to generate a dynamic registration form, based on on specific client needs. I've created a UserProfile model with most of the fields set as blank=True.

生成表单时,我从另一个数据库表中提取了客户端指定的字段,然后生成注册表。所有这些工作正常,除了所有字段都允许使用空白值。到目前为止,我有这个

When the form gets generated, I pull the client specified fields from another db table and generate the registration form. All this works, except that all the fields allow blank values. So far I have this

def RegProfileForm(include_list, *args, **kwargs):
    class ProfileForm(forms.ModelForm):
        class Meta:
            model = hr.UserProfile
            fields = include_list

        def __init__(self):
            super(ProfileForm, self).__init__(*args, **kwargs)

    return ProfileForm()

然后我这样称呼此表单:

Then I call this form like this:

includes = ['gender','work_phone'] # dynamic fields
of = RegProfileForm(includes)

如何动态地从某些指定的字段中删除blank = True要求

How do I dynamically remove the blank=True requirement from certain specified fields during runtime, or when I generate the form?

推荐答案

我发现我可以做这样的事情来覆盖默认值来自模型的值:

I figured out I can just do something like this to override the default values from the model:

form = MyAuthForm(data)
form.fields['first_name'].required = True
form.fields['email'].required = False

这篇关于模型中的动态表单要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆