Django智能选择数据检查

2024-06-07 07:42:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在开发一个使用pythondjango的web应用程序系统,并使用Django Smart\u Select作为我的个人资料编辑页面 在配置文件编辑页面下,每个用户配置文件都有国家、州和议会

有些州没有协议(举个例子) 所以,在注册新用户时,我会检查,如果用户选择了包含议会的州,那么议会字段是必需的,否则该字段可以为空。你知道吗

在表单.py我的状态模型使用ModelChoiceField

我在这上面呆了几个小时,请帮帮我。。。非常感谢,非常感谢您的合作。你知道吗


Tags: django用户web应用程序编辑协议smart系统
1条回答
网友
1楼 · 发布于 2024-06-07 07:42:42

documentation

class ExampleForm(forms.Form):

parliament = …
state = ModelChoiceField(…)

def clean_parliament(self):
    if self.cleaned_data[“state”].has_parliament() and not self.cleaned_data[“parliament”]:
        raise forms.ValidationError(“parliament is required for this state!”)

    return self.cleaned_data[“parliament”]

这假设您的State模型有一个has_parliament方法,该方法根据州是否有议会返回True或False。您可以修改上面的代码来执行任何其他类型的错误检查。你知道吗

相关问题 更多 >

    热门问题