【发布时间】:2019-06-14 20:54:42
【问题描述】:
我尝试为我的 clean_ 方法编写一个测试。
这是我的测试代码
def test_clean_restraints(self):
form = NewTaskForm(dict(restraints="90 20 <>"))
form.clean_restraints()
在这一步我收到一个错误:
Error
Traceback (most recent call last):
File "/home/user/django_projects/my_webservice/tasks/tests/test_forms.py", line 12, in test_clean_restraints
form.clean_restraints()
File "/home/user/django_projects/my_webservice/tasks/forms.py", line 22, in clean_restraints
if self.cleaned_data.get('restraints') == '':
AttributeError: 'NewTaskForm' object has no attribute 'cleaned_data'
NewTaskForm 如下所示:
class NewTaskForm(ModelForm):
class Meta:
model = Task
restraints = forms.CharField()
region = forms.CharField()
interactions = forms.CharField()
def clean_restraints(self):
if self.cleaned_data.get('restraints') == '':
return self.cleaned_data.get('restraints')
data = self.cleaned_data.get('restraints').strip().split('\n')
regexp = re.compile(r'^(\d+)[\t ]+(\d+)[ \t]+([><]{2})?$')
cleaned_data = []
for i, line in enumerate(data):
match = regexp.match(line)
if not match:
raise forms.ValidationError(f"Error in restraints in line {i + 1}")
else:
rst_1, rst_2, loop_type = match.groups()
rst_1 = int(rst_1)
rst_2 = int(rst_2)
cleaned_data.append((rst_1, rst_2, loop_type))
return cleaned_data
我正在使用 Django 2.1、python 3.7.1、PyCharm 2018.3.3 Professional 我试图在 PyCharm 的调试器下运行它,但事情变得疯狂。我收到不同的错误信息。看起来调试器在忽略断点的完整表单验证后停止了。我不知道发生了什么。
【问题讨论】:
-
请注意,当您在 PyCharm 中调试表单清理方法时,您必须注意不要在调用清理方法本身之前的任何位置设置断点。那是因为调试器本身想要内省您的表单并将调用
form.errors来执行此操作(调用form.is_valid()调用form.full_clean()...)所以clean中的断点不再到达,因为form._errors是已经有人了。
标签: django django-forms django-testing