想使用内置的list()将字典转换为django rest框架视图内的列表,但如果我尝试AttributeError: 'dict_keys' object has no attribute 'META'.
或.values()
,它会引发.items()
相同。无论将字典放在def list
方法中还是覆盖__init__
并将其放在那里,错误仍然存在。 django之外的相同代码似乎可以正常工作,那么这是怎么回事?它与名为“列表”的DRF方法有关吗?
以下是引发错误的简化版本:
class SomeViewSet(viewsets.ViewSet):
some_dict = {
'key1': 'value1',
'key2': 'value2'
}
def list(self, request, format=None):
print(list(self.some_dict.keys()))
return Response(some_response())
这很正常:
class SomeViewSet(viewsets.ViewSet):
some_dict = {
'key1': 'value1',
'key2': 'value2'
}
def list(self, request, format=None):
print(self.some_dict.keys())
return Response(some_response())
使用[]代替list()可以工作,但是它只是将视图对象放在列表中,而不是将其转换为列表,所以这不好。 跟踪:
Traceback (most recent call last):
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\core\management\base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\core\management\base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\core\checks\registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\urls\resolvers.py", line 396, in check
for pattern in self.url_patterns:
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\utils\functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\urls\resolvers.py", line 533, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\utils\functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\urls\resolvers.py", line 526, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "E:\Github\MapPoolApp\MapPoolApp\urls.py", line 20, in <module>
path('', include('mappool.urls')),
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "E:\Github\MapPoolApp\mappool\urls.py", line 2, in <module>
from . import views
File "E:\Github\MapPoolApp\mappool\views.py", line 188, in <module>
class MapPoolViewSet(viewsets.ViewSet):
File "E:\Github\MapPoolApp\mappool\views.py", line 205, in MapPoolViewSet
place = list(pool_args.items())
File "E:\Github\MapPoolApp\mappool\views.py", line 49, in list
return render(request, 'mappool/list.html', context)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\template\base.py", line 169, in render
with context.bind_template(self):
File "C:\Users\FluffyAdmin\AppData\Local\Programs\Python\Python36-32\Lib\contextlib.py", line 82, in __enter__
return next(self.gen)
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\template\context.py", line 246, in bind_template
updates.update(processor(self.request))
File "C:\Users\FluffyAdmin\.virtualenvs\MapPoolApp-gFHC46op\lib\site-packages\django\template\context_processors.py", line 40, in debug
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
AttributeError: 'dict_items' object has no attribute 'META'
是DRF魔术吗,还是我在这里缺少明显的东西?