【问题标题】:Django Modelform giving isinstance() arg 2 must be a class, type, or tuple of classes and types errorDjango Modelform 给出 isinstance() arg 2 must be a class, type, or tuple of classes and types error
【发布时间】:2011-04-25 06:58:37
【问题描述】:

在特定视图上出现以下错误:

isinstance() arg 2 must be a class, type, or tuple of classes and types

Request Method:     GET
Request URL:    ***********CHANGED FOR PRIVACY
Django Version:     1.3
Exception Type:     TypeError
Exception Value:    

isinstance() arg 2 must be a class, type, or tuple of classes and types

Exception Location:     /usr/lib/python2.6/site-packages/Django-1.3-py2.6.egg/django/db/models/fields/related.py in get_default, line 867
Python Executable:  /usr/bin/python

同样的观点在我的本地开发环境中没有给出任何错误。 该站点部署在运行 apache 的 Amazon Ec2 上。

这是引发异常的确切代码:
form = MyCustomModelForm() # An unbound form

【问题讨论】:

    标签: django django-models django-forms


    【解决方案1】:

    奇怪的巧合,但我相信你的问题与to this question密切相关。

    我会尝试那里发布的相同解决方案:在您的 ModelForm 之前强制 get_models

    from django.db.models.loading import cache as model_cache
    if not model_cache.loaded:
        model_cache.get_models()
    

    您的ModelForm 是否在您的模型相关应用尚未完成加载的位置实例化?

    这是另一个例子:这个人有一个中间件在字符串被解析为对象之前使用外键字符串访问模型,并且它只出现在 apache 上也有同样的问题——据他说是因为开发环境先运行get_models() - 不知道。

    http://groups.google.com/group/django-users/msg/d1482a2eba45e498?dmode=source

    【讨论】:

    • 我要试试这个。我没有使用管理员功能,因此管理员自动发现不会出错。好奇如果我使用自动发现是否会得到同样的错误。
    • 是的,如果我在INSTALLED_APPS 列表中早先定义了一个模型,该模型通过外键字符串引用了更远的模型,并且我实例化了模型类,我可以重现您的错误。希望这就是问题所在!
    • 原来如此!你太棒了!我不知道模型表单和管理员自动发现问题的联系。也许如果我对内部了解更多一点。
    • 我在 django shell 中遇到了这个问题。我已经将 model_cache.get_models() 放在了两个 models.py 中,调整了 ISTALLED_APPS 并且我无法摆脱它。唯一有帮助的是在创建所需的类之前导入两个应用程序: from app1.models import Cls1 ;从 app2.import Cls2 ; a = Cls2()
    【解决方案2】:

    试试这个:

    self.assertIsInstance(Inst,pywbem.cim_obj.CIMInstance,"input Instance {} is not a CIMInstance object".format(Inst))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 2020-03-23
      • 1970-01-01
      • 2017-12-03
      相关资源
      最近更新 更多