AttributeError:当使用django rest框架运行生菜时,'NoneType'对象没有属性'_default_manager'

时间:2014-01-15 23:21:55

标签: python django django-rest-framework lettuce

我正在尝试使用vend来实现BDD来创建一个rest框架api但是当我执行命令python manage.py harvest时出现以下错误

/rest_framework/serializers.py", line 818, in get_related_field
    'queryset': related_model._default_manager,
AttributeError: 'NoneType' object has no attribute '_default_manager'

详细的堆栈跟踪是:

(envSamplePayApp)root@ubuntu:/pythonWorkspace/samplePayProject# python manage.py harvest
Django's builtin server is running at 0.0.0.0:8000
Traceback (most recent call last):
  File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/lettuce/django/management/commands/harvest.py", line 167, in handle
    result = runner.run()
  File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/lettuce/__init__.py", line 137, in run
    self.loader.find_and_load_step_definitions()
  File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/lettuce/fs.py", line 49, in find_and_load_step_definitions
    module = __import__(to_load)
  File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/tests/test_serializer_import.py", line 4, in <module>
    from rest_framework.tests.accounts.serializers import AccountSerializer
  File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/tests/accounts/serializers.py", line 6, in <module>
    class AccountSerializer(serializers.ModelSerializer):
  File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/tests/accounts/serializers.py", line 8, in AccountSerializer
    admins = UserSerializer(many=True)
  File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 196, in __init__
    self.fields = self.get_fields()
  File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 232, in get_fields
    default_fields = self.get_default_fields()
  File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 716, in get_default_fields
    field = self.get_related_field(model_field, related_model, to_many)
  File "/pythonWorkspace/samplePayProject/envSamplePayApp/local/lib/python2.7/site-packages

所有代码都可以在downloadcode找到。

请注意,当我从浏览器运行api时,它可以正常工作。

导致AttributeError: 'NoneType' object has no attribute '_default_manager'的原因是什么?

1 个答案:

答案 0 :(得分:1)

我刚才遇到了这个错误。我的错误的原因是因为我已经从App A的模型X到App B的模型Y创建了ManyToManyField。当我尝试通过python manage.py schemamigration --auto app_a创建模式迁移时,它提出了这个问题。错误,因为我还没有:

  1. 'app_b' <{1}}
  2. INSTALLED_APPS元组中加入settings.py
  3. 尚未为app_b创建初始架构迁移(将app_b添加到INSTALLED_APPS是不可能的)。
  4. 有效地,我链接到当时不为人知的模型。希望这可以帮助将来的某个人。

    编辑:实际上,在进一步检查时,我现在意识到由于django-debug-toolbar 而导致此错误被引起歧视的原因。显然DjangoDebugToolbar导致错误传播正确的一些问题。在INSTALLED_APPS中禁用它可以使错误变得明显。