我花了很多时间试图弄清楚为什么下面的代码不能正常工作。我搜索过Python文档和Google,并阅读了django文档,但我仍然无法理解它为什么不起作用。在
基本上,我有一个命名元组来存储所需的字段数据。然后,我想在条目保存/更新到数据库之前调用命名元组来清除表单中的数据。在
例如,当用户选择的地址类型为6时,LocalityDisplay为False。然后在表单的验证中,if-atype_config.LocalityDisplay==False:应该被触发,并且address_locality中的值应该被删除,这意味着对于address_locality不应该将任何值保存到数据库中。我用了返回值.strip()否则在我的表单.py而且效果很好。在
我不确定我是否在编码命名元组时犯了一个基本错误,导致命名元组没有被正确索引,或者我是否编码了if-atype_config.LocalityDisplay==False:条件不正确或出现了其他我看不到的错误。在
没有显示错误,如果输入_config.LocalityDisplay==False:显示没有跳闸/到达。在
这是我的代码:
表单.py
from collections import namedtuple
RequiredFields = namedtuple('RequiredFields', ['LocalityDisplay', 'LocalityRequired', 'RegionDisplay', 'RegionRequired', 'PostcodeDisplay', 'PostcodeRequired'])
........... (do some stuff here)
class AddressDetailsForm(forms.ModelForm):
........... (do some stuff here)
REQUIRED_FIELDS = {
0: RequiredFields(LocalityDisplay=True, LocalityRequired=False, RegionDisplay=True, RegionRequired=False, PostcodeDisplay=True, PostcodeRequired=False),
1: RequiredFields(LocalityDisplay=False, LocalityRequired=False, RegionDisplay=True, RegionRequired=True, PostcodeDisplay=True, PostcodeRequired=True),
2: RequiredFields(LocalityDisplay=True, LocalityRequired=True, RegionDisplay=False, RegionRequired=False, PostcodeDisplay=True, PostcodeRequired=True),
3: RequiredFields(LocalityDisplay=True, LocalityRequired=True, RegionDisplay=False, RegionRequired=False, PostcodeDisplay=True, PostcodeRequired=True),
4: RequiredFields(LocalityDisplay=True, LocalityRequired=True, RegionDisplay=False, RegionRequired=False, PostcodeDisplay=True, PostcodeRequired=True),
5: RequiredFields(LocalityDisplay=True, LocalityRequired=True, RegionDisplay=True, RegionRequired=True, PostcodeDisplay=True, PostcodeRequired=True),
6: RequiredFields(LocalityDisplay=False, LocalityRequired=False, RegionDisplay=True, RegionRequired=True, PostcodeDisplay=True, PostcodeRequired=True),
........... (culled for brevity)
285: RequiredFields(LocalityDisplay=True, LocalityRequired=True, RegionDisplay=False, RegionRequired=False, PostcodeDisplay=False, PostcodeRequired=False)
}
........... (do some stuff here)
cd_addf = super(AddressDetailsForm, self).clean()
address_type = cd_addf.get('address_country_style_type', None)
if address_type is None:
# Prior validation will spot this
pass
elif address_type == 0:
self._errors['address_country_style_type'] = self.error_class([_("You must select an Address Format.")])
else:
atype_config = REQUIRED_FIELDS[address_type]
........... (do some stuff here)
if atype_config.LocalityDisplay == False:
def clean_address_locality(self):
value = self.cleaned_data['address_locality']
return value.strip()
........... (do some stuff here)
return cd_addf
我问了一个相关的问题here,但是没有解决这个问题,所以我不确定是什么原因导致代码不能工作。在
这似乎与你的namedtuple没有任何关系。如果您按照评论中的建议添加一些日志记录,您就可以看到这些。问题完全在if语句中,代码根本没有意义。在
首先,正如你在另一个问题中详细解释的那样,
strip
并不像你想象的那样。它返回末尾去掉空格的值。如果希望clean函数返回None,那么只需返回None,不要使用strip。在其次,更重要的是,在另一个方法的if语句中定义函数是完全没有意义的。Django对新方法一无所知,不能从外部调用另一个函数内部定义的函数,即使可以,Django在调用main
clean
方法时已经完成了clean_fieldname
函数。在但在任何情况下,所有这些都是毫无意义的;您已经可以访问
cleaned_data
字典中的clean
方法,那么为什么不简单地删除其中的元素呢?在相关问题 更多 >
编程相关推荐