简体   繁体   English

Django-从模型中强制去除CharField中的空格

[英]Django - enforce stripping whitespaces in CharField from the model

I am trying to populate data into the following model from a csv and using update_or_create() in a loop to do that. 我试图将数据从csv填充到以下模型中,并在循环中使用update_or_create()来做到这一点。

class TargetPlace(models.Model):
    name = models.CharField(max_length=100)

The csv may have some entries which have leading or trailing white spaces and I want to get rid of those. CSV可能有一些带有前导或尾随空格的条目,我想摆脱这些条目。

I saw that CharField's in Django >=1.9 have a strip argument that takes care of this by default (since its True by default). 我看到Django> = 1.9中的CharField具有一个strip参数,默认情况下会处理该参数(因为默认情况下为True)。 I am using Django 1.10 but still seeing duplicate entries in the model (those with and without leading white spaces). 我正在使用Django 1.10,但仍然在模型中看到重复的条目(带有和不带有前导空格的条目)。

Does the strip argument not work that way? 剥离参数不能这样工作吗?

I guess I got confused with the CharField in a Form vs CharField in a Model. 我想我对Form中的CharField与Model中的CharField感到困惑。 CharField in a Model does not seem to have any such argument 模型中的CharField似乎没有任何这样的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM