【问题标题】:Leave DECIMALFIELD blank if criteria is not met如果不满足条件,请将 DECIMAFIELD 留空
【发布时间】:2017-04-27 11:53:52
【问题描述】:

我正在尝试将 DECIMAFIELD 留空。如果不符合条件,我希望我的“if 语句”将该字段留空。我的模型已经设置为blank=True, null=True

def clean_xrate(self):
    xrate_date = self.cleaned_data.get(str("date"))
    currency = self.cleaned_data.get("currency")
    curr = currency
    if not xrate_date:
        raise ValidationError('Error')
    elif curr == 'USD':
        xrate = 'NULL' <---This doesn't work. ☹️
    else:
        prior_date_rate = cr.get_rate(curr, 'USD', xrate_date)
        xrate = "{0:.4f}".format(prior_date_rate)

    return xrate

提前感谢您的帮助!

【问题讨论】:

  • xrate = None?

标签: django python-3.x django-models django-forms


【解决方案1】:

None 在 python 中将在数据库中生成NULL(大多数 dbs)。所以你可以这样做

xrate = None

【讨论】:

  • 完美。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
相关资源
最近更新 更多