0

嗨,我添加了自定义验证。它抛出一个 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 103 中的文件“C:\Python27\lib\site-packages\django\core\handlers\base.py”。resolver_match = resolver.resolve(request.path_info) 文件“C:\Python27\lib\site- packages\django\core\urlresolvers.py" 在解析 319 中。对于 self.url_patterns 中的模式: url_patterns 347 中的文件 "C:\Python27\lib\site-packages\django\core\urlresolvers.py"。patterns = getattr( self.urlconf_module, "urlpatterns", self.urlconf_module) 文件 "C:\Python27\lib\site-packages\django\core\urlresolvers.py" 在 urlconf_module 342. self._urlconf_module = import_module(self.urlconf_name) 文件 "C :\Python27\lib\site-packages\django\utils\importlib.py" 在 import_module 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) 文件 "C:\Python27\lib\site-packages\django\utils\importlib.py" 在 import_module 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) File "C:\Python27\lib\site-packages\django\contrib\admin\validation.py" 在 validate 25. validate_base(cls, model) File "C :\Python27\lib\site-packages\django\contrib\admin\validation.py" 在 validate_base 317 中。如果 hasattr(cls, 'form') 而不是 issubclass(cls.form, BaseModelForm):

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

4

1 回答 1

1

请参阅下面的 admin.py 代码

    从 .forms 导入 tbmstappraisalschedForm
    类 AppraisalScheduleAdmin(admin.ModelAdmin):
        排除 = ['intUpdatedBy']
    #form = 'tbmstappraisalschedForm' 你不能在这里传递字符串。
         form = tbmstappraisalschedForm #这样做

def save_model(self, request, obj, form, change): obj.intUpdatedBy = request.user.id obj.save()
于 2013-10-25T12:30:57.253 回答