获取错误对象没有属性' get'在django modelform中使用clean

时间:2016-05-17 06:01:13

标签: django django-forms

我正在尝试自定义验证我的模型表单。为此,我写了以下代码:

class StudentForm(forms.ModelForm):

    class Meta:
        model = Student
        fields = '__all__'

    def clean(self):

        batch_start_year = self.cleaned_data.get('batch_start_year',None)

我收到的错误如下:

'StudentForm' object has no attribute 'get'

我尝试了另一种解决方案,但它也没有用。

class StudentForm(forms.ModelForm):

    class Meta:
        model = Student
        fields = '__all__'

    def clean(self):
        cleaned_data = super(StudentForm, self).clean()

        batch_start_year = cleaned_data['batch_start_year']

请帮我解决这个问题。

完整堆栈跟踪:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/shahjahan/Desktop/jmialumniusa/jmialumniusa_app/views.py", line 18, in apply
    if form.is_valid():
  File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py", line 161, in is_valid
    return self.is_bound and not self.errors
  File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py", line 153, in errors
    self.full_clean()
  File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py", line 364, in full_clean
    self._post_clean()
  File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 377, in _post_clean
    exclude = self._get_validation_exclusions()
  File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 337, in _get_validation_exclusions
    field_value = self.cleaned_data.get(field)
AttributeError: 'StudentForm' object has no attribute 'get'

2 个答案:

答案 0 :(得分:0)

您应该从clean()方法返回已清理的数据或引发错误。你不这样做。

class StudentForm(forms.ModelForm):

    class Meta:
        model = Student
        fields = '__all__'

    def clean(self):
        batch_start_year = self.cleaned_data.get('batch_start_year',None)
        # do something
        return self.cleaned_data

答案 1 :(得分:0)

如果您想验证所有表单,可以使用这样的干净方法

class StudentForm(forms.ModelForm):

    class Meta:
        model = Student
        fields = '__all__'

    def clean(self):
        cleaned_data = super(StudentForm, self).clean()
        batch_start_year = cleaned_data.get('batch_start_year')

在这种情况下,您无需返回任何内容。您只会提出验证错误。如果您想验证某些特定字段,您可以这样做

def clean_field_name(self):
    data = self.cleaned_data.get('field_name')
    if "something" not in data:
        raise forms.ValidationError("Validation message")

    # Always return the cleaned data, whether you have changed it or
    # not.
    return data

另一种可能的错误可能是您向表单发送数据的方式

student = Student.objects.get(pk=id)
form = StudentForm(intention) # An unbound form

表单的第一个参数是数据,但是您正在传递实例。要正确传递实例,您应该使用:

student = Student.objects.get(pk=id)
form = StudentForm(instance=student) #