Skip to main content
 首页 » 编程设计

python之属性错误 : 'NoneType' object has no attribute '_default_manager' when running lettuce with django rest framework

2025年05月04日24mfryf

我正在尝试使用生菜来实现 BDD 以创建一个休息框架 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'?

请您参考如下方法:

我刚才遇到了这个错误。我出错的原因是我创建了一个 ManyToManyField从应用程序 A 的模型 X 到应用程序 B 的模型 Y。当我尝试通过 python manage.py schemamigration --auto app_a 创建模式迁移时,它引发了这个错误,因为我还没有:

  1. 包括'app_b'在INSTALLED_APPS settings.py 的元组
  2. 没有为 app_b 创建初始模式迁移(将 app_b 添加到 INSTALLED_APPS 是不可能的)。

实际上,我链接到一个当时未知的模型。希望这可以帮助将来的人。

编辑:实际上,经过进一步检查,我现在意识到 如此模棱两可地引发此错误的原因是因为 django-debug-toolbar 。显然 DjangoDebugToolbar 导致了一些错误正确传播的问题。在 INSTALLED_APPS 中禁用它让错误变得明显。