django strip / remove / clear值 [英] django strip/remove/clear value

查看:270
本文介绍了django strip / remove / clear值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.6.7& python 2.7。



如果在数据提交到数据库之前,我正在尝试清除字段中的数据。



我一直在使用strip,但这不再工作。搜索django文档后,我找不到解决方案。



这是我的forms.py文件中的代码,其中strip()不起作用:

 如果atype_config.LocalityDisplay:
def clean_address_locality(self):
value = self.cleaned_data ['address_locality']
return value.strip()


解决方案

答案是不是用户条。使用pop()代替。



这是代码:

 如果不是atype_config.LocalityDisplay:
self.cleaned_data.pop('address_locality',None)

我希望这有助于某人。


I am using django 1.6.7 & python 2.7.

I am trying to clear the data in a field if it fits a condition before the value is submitted to the database.

I had been using strip, but this no longer works. After searching the django docs I cannot find a solution.

Here is my code in my forms.py file, where strip() does not work:

        if atype_config.LocalityDisplay:
            def clean_address_locality(self):
                value = self.cleaned_data['address_locality']
                return value.strip()

解决方案

The answer is not to user strip. Use pop() instead.

Here is the code:

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

I hope that this helps someone.

这篇关于django strip / remove / clear值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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