【问题标题】:django namedtuple not returning valuedjango namedtuple 不返回值
【发布时间】:2014-10-29 22:59:29
【问题描述】:

我花了很多时间试图弄清楚为什么以下代码不能正常工作。我搜索了 SO、Python 文档和 Google 并阅读了 django 文档,但我仍然无法弄清楚为什么它不起作用。

基本上,我有一个命名元组来存储必填字段数据。然后我想在条目被保存/更新到数据库之前调用命名元组以清除表单中的数据。

例如,当用户选择的address_type为6时,则LocalityDisplay为False。然后在表单的验证中,应该触发 if atype_config.LocalityDisplay == False: 并且应该删除 address_locality 中的值,这意味着不应将值保存到数据库中 address_locality。我在 forms.py 的其他地方使用了 return value.strip() 并且效果很好。

我不确定我在对命名元组进行编码时是否犯了基本错误,导致命名元组无法正确索引,或者我是否对 if atype_config.LocalityDisplay == False: 条件进行了错误编码或犯了其他一些我看不到的错误。

没有显示错误,if atype_config.LocalityDisplay == False: 似乎没有被绊倒/到达。

这是我的代码:

forms.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,但没有解决问题,所以我不确定代码不起作用的原因。

【问题讨论】:

  • 你确定address_type 不是None0
  • schillingt,我通过删除 if else 条件并只有 if atype_config.LocalityDisplay == False: 条件来查看它。问题仍然存在。
  • 您是否尝试添加一些日志以查看执行情况与预期不符?

标签: python django namedtuple


【解决方案1】:

这似乎与您的命名元组没有任何关系。如果您按照评论中的建议添加一些日志记录,您将能够看到这一点。问题完全在 if 语句中,代码毫无意义。

首先,正如您在另一个问题中详细解释的那样,strip 并没有像您认为的那样做。它返回末尾去掉空格的值。如果你希望你的 clean 函数返回 None,那么只返回 None,不要使用 strip。

其次,更重要的是,在另一个方法中的 if 语句中定义函数是完全没有意义的。 Django 对你的新方法一无所知,你不能从外部调用另一个函数内部定义的函数,即使你可以在调用主 clean 时 Django 已经完成了 clean_fieldname 函数方法。

但无论如何,这一切都是毫无意义的;您已经可以在clean 方法中访问cleaned_data 字典,那么为什么不简单地删除那里的元素呢?

if not atype_config.LocalityDisplay:
    self.cleaned_data.pop('address_locality', None)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 2020-12-23
    • 2022-07-20
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多