您好我的自定义验证抛出一个TypeError,说issubclass()arg 1必须是一个类

时间:2013-10-25 12:19:20

标签: django django-forms django-admin

您好我添加了自定义验证。它抛出一个TypeError,表示issubclass()arg 1必须是一个类。我在admin.py中的代码是

class AppraisalScheduleAdmin(admin.ModelAdmin):
    exclude = ['intUpdatedBy']
    form = 'tbmstappraisalschedForm'

        def save_model(self, request, obj, form, change):
            obj.intUpdatedBy = request.user.id
            obj.save()

class tbmstappraisalschedForm(forms.ModelForm):

     def clean(self):
          """ 
          Override the default clean method to check whether this course has been already inputted.
          """    
          cleaned_data = super(tbmstappraisalschedForm, self).clean()
          depart_id = cleaned_data.get('intDeptID')
          fromdate = cleaned_data.get('sdtFromDate')
          todate = cleaned_data.get('todate')
          matching_records = tbmstappraisalsched.objects.filter(intDeptID=depart_id)
          if self.instance:
              matching_records.exclude(pk=self.instance.pk)
          if matching_records.exists():
              msg = "The slot for selected department and selected dates exists" % name
              raise ValidationError(msg)
          else:
              return self.cleaned_data
    class Meta:
        model = tbmstappraisalsched

追溯是

回溯: get_response中的文件“C:\ Python27 \ lib \ site-packages \ django \ core \ handlers \ base.py”   103. resolver_match = resolver.resolve(request.path_info) 解析文件“C:\ Python27 \ lib \ site-packages \ django \ core \ urlresolvers.py”   319.对于self.url_patterns中的模式: url_patterns中的文件“C:\ Python27 \ lib \ site-packages \ django \ core \ urlresolvers.py”   347. patterns = getattr(self.urlconf_module,“urlpatterns”,self.urlconf_module) urlconf_module中的文件“C:\ Python27 \ lib \ site-packages \ django \ core \ urlresolvers.py”   342. self._urlconf_module = import_module(self.urlconf_name) import_module中的文件“C:\ Python27 \ lib \ site-packages \ django \ utils \ importlib.py”   35. 导入(姓名) 文件“C:\ pythonprgs \ opas \ opas \ urls.py”中   5. admin.autodiscover() 自动发现中的文件“C:\ Python27 \ lib \ site-packages \ django \ contrib \ admin__init __。py”   29. import_module('%s.admin'%app) import_module中的文件“C:\ Python27 \ lib \ site-packages \ django \ utils \ importlib.py”   35. 导入(姓名) 文件“C:\ pythonprgs \ opas \ schdeules \ admin.py”中   60. admin.site.register(tbmstappraisalsched,AppraisalScheduleAdmin) 在寄存器中输入“C:\ Python27 \ lib \ site-packages \ django \ contrib \ admin \ sites.py”   98. validate(admin_class,model) 在验证中文件“C:\ Python27 \ lib \ site-packages \ django \ contrib \ admin \ validation.py”   25. validate_base(cls,model) validate_base中的文件“C:\ Python27 \ lib \ site-packages \ django \ contrib \ admin \ validation.py”   317.如果hasattr(cls,'form')而不是issubclass(cls.form,BaseModelForm):

异常类型:/ admin / schdeules / tbmstappraisalsched / add /中的TypeError 异常值:issubclass()arg 1必须是类

1 个答案:

答案 0 :(得分:1)

请参阅以下admin.py代码     

    from .forms import tbmstappraisalschedForm 
    class AppraisalScheduleAdmin(admin.ModelAdmin):
        exclude = ['intUpdatedBy']
    #    form = 'tbmstappraisalschedForm'  you can not pass a string  here .
         form = tbmstappraisalschedForm     #do like this

def save_model(self, request, obj, form, change): obj.intUpdatedBy = request.user.id obj.save()