清理类型为URLField的数据 [英] Cleaning data which is of type URLField
问题描述
我的模型中有一个简单的URLField
I have a simple URLField in my model
link = models.URLField(verify_exists = False, max_length = 225)
我想从字段中删除前导和尾随空格。我不认为我可以在clean_fieldname或clean方法中执行此操作。
I would like to strip the leading and trailing spaces from the field. I don't think I can do this in "clean_fieldname" or in the "clean" method.
我需要对URLField进行子类化,并删除to_python方法中的空格吗?有没有更好的方式来做这个没有任何分类?
Do I need to sub-class the "URLField" and remove the spaces in to_python method? Is there a better way to do this without any sub-classing?
已编辑
这是我的表单
class StoryForm(forms.ModelForm):
title = forms.CharField(max_length=225, error_messages={'required' : 'Enter a title for the story'})
link = forms.URLField(max_length=225, error_messages={'required' : 'Enter a link to the story', 'invalid' : 'Enter a valid link like www.ted.com'})
class Meta:
model = models.Story
fields = ('title', 'link')
def clean_link(self):
link = self.cleaned_data['link']
return link.strip()
和我的模型
class Story(models.Model):
title = models.CharField(max_length = 225)
link = models.URLField(verify_exists = False, max_length = 225)
推荐答案
我做了一个快速实验,发现您可以使用 clean _
方法来删除前导/尾随空格。如下所示:
I did a quick experiment and found out that you can indeed use a clean_
method to remove leading/trailing spaces. Something like this:
# models.py
class UrlModel(models.Model):
link = models.URLField(verify_exists = False, max_length = 225)
def __unicode__(self):
return self.link
# forms.py
class UrlForm(ModelForm):
class Meta:
model = UrlModel
def clean_link(self):
link = self.cleaned_data['link']
return link.strip()
# shell
In [1]: from test_app.forms import UrlForm
In [2]: f = UrlForm(data = dict(link = ' http://google.com '))
In [3]: f.is_valid()
Out[3]: True
In [4]: f.save()
Out[4]: <UrlModel: http://google.com>
更新
我收到一条错误,说输入有效的链接,如www.ted.com。我编辑了我的问题,并包含了有关的模型和表单。
I get an error saying "Enter a valid link like www.ted.com". I edited my question and included the model and form in question.
我验证了您的表单类确实提供了错误。
I verified that your form class does give the error.
在做了一个小的改动后,我能够使其工作。我所做的是删除自定义标题
和链接
字段。我们正在使用这里的模型表单,底层模型已经有这些领域。我相信重新定义会导致在调用自定义清除方法之前提出验证错误。
After making a small change I was able to make it work. All I did was remove the custom title
and link
fields. We are working with a model form here and the underlying model already has these fields. I believe the redefinition led to a validation error being raised before the custom clean method was invoked.
class StoryForm(forms.ModelForm):
class Meta:
model = Story
fields = ('title', 'link')
def clean_link(self):
link = self.cleaned_data['link']
return link.strip()
这是Shell的一些示例输出:
Here is some sample output from the shell:
In [1]: from test_app.forms import StoryForm
In [2]: data = dict(title="Google story", link = " http://google.com ")
In [3]: f = StoryForm(data)
In [4]: f.is_valid()
Out[4]: True
In [5]: f.save()
Out[5]: <Story: Google story http://google.com>
这篇关于清理类型为URLField的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!