APIRequestFactoryの代わりにAPIClientクラスを使用して、新しく作成したビューセットで簡単なテストを実行しようとしていますが、Django Rest Framework AttributeError: 'レスポンス'オブジェクトに 'エンコーディング'属性がありません
AttributeError: 'Response' object has no attribute 'encoding'
私もAPIRequestFactoryを使用する場合は正常に動作しますが、私はTokenAuthenticationを使用していますので、私はAPIClientを必要としています。
これは、私がクライアントを使用していたコンテキストです:
api_client = APIClient()
reversed_url = reverse('showcase:ajax:publisher-dashboard', kwargs={'pk': 1})
client_token = cm.Client.objects.get(first_name='Cliente 1',
last_name='Prueba Showcase').user.auth_token.key
api_client.credentials(HTTP_AUTHORIZATION='Token ' + client_token)
request = api_client.get(reversed_url, format='json')
response = views.PubViewSet.as_view({'get': 'dashboard'})(request, pk=1)
は結果が資格情報を提供し、settings.pyにTokenAuthを無効にすることなく同じです。
APIRequestFactoryを使用している場合、これらは、Responseオブジェクトの内容は以下のとおりです。
<Response status_code=200, "text/html; charset=utf-8">
_charset = {NoneType} None
_closable_objects = {list} <class 'list'>: []
_container = {list} <class 'list'>: [b'']
_content_type_for_repr = {str} ', "text/html; charset=utf-8"'
_handler_class = {NoneType} None
_headers = {dict} {'content-type': ('Content-Type', 'text/html; charset=utf-8')}
_is_rendered = {bool} False
_post_render_callbacks = {list} <class 'list'>: []
_reason_phrase = {NoneType} None
_request = {NoneType} None
charset = {str} 'utf-8'
closed = {bool} False
content = {str} 'Traceback (most recent call last):\n File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n attr = getattr(var, n)\n File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag
content_type = {NoneType} None
context_data = {NoneType} None
cookies = {SimpleCookie}
data = {dict} {'publisher': {'id': 1, 'first_name': '', 'last_name': '', 'name': 'TEST', 'official_name': 'TEST, S.A.', 'rfc': 'XAXXXXXXX000X', 'street': 'St.', 'number': '420', 'interior': '', 'neighborhood': '', 'zipcode': '', 'email': '', 'phone': '', 'image': '/rcro
exception = {bool} False
is_rendered = {bool} False
reason_phrase = {str} 'OK'
rendered_content = {str} 'Traceback (most recent call last):\n File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n attr = getattr(var, n)\n File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag
rendering_attrs = {list} <class 'list'>: ['template_name', 'context_data', '_post_render_callbacks']
status_code = {int} 200
status_text = {str} 'OK'
streaming = {bool} False
template_name = {NoneType} None
using = {NoneType} None
そしてAPIClientが使用されます。
<Response status_code=200, "text/html; charset=utf-8">
_charset = {NoneType} None
_closable_objects = {list} <class 'list'>: []
_container = {list} <class 'list'>: [b'']
_content_type_for_repr = {str} ', "text/html; charset=utf-8"'
_handler_class = {NoneType} None
_headers = {dict} {'content-type': ('Content-Type', 'text/html; charset=utf-8')}
_is_rendered = {bool} False
_post_render_callbacks = {list} <class 'list'>: []
_reason_phrase = {NoneType} None
_request = {NoneType} None
charset = {str} 'utf-8'
closed = {bool} False
content = {str} 'Traceback (most recent call last):\n File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n attr = getattr(var, n)\n File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag
content_type = {NoneType} None
context_data = {NoneType} None
cookies = {SimpleCookie}
data = {dict} {'publisher': {'id': 1, 'first_name': '', 'last_name': '', 'name': 'TEST', 'official_name': 'TEST, S.A.', 'rfc': 'XAXXXXXXX000X', 'street': 'St.', 'number': '420', 'interior': '', 'neighborhood': '', 'zipcode': '', 'email': '', 'phone': '', 'image': '/rcro
exception = {bool} False
is_rendered = {bool} False
reason_phrase = {str} 'OK'
rendered_content = {str} 'Traceback (most recent call last):\n File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n attr = getattr(var, n)\n File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag
rendering_attrs = {list} <class 'list'>: ['template_name', 'context_data', '_post_render_callbacks']
status_code = {int} 200
status_text = {str} 'OK'
streaming = {bool} False
template_name = {NoneType} None
using = {NoneType} None
私はそれがAPIRequestFactoryの代わりにドロップされたと思ったが、実際には指定されたURLへの完全なget要求を生成します。これを確認するには、[Djangoの元のクライアントクラス](https://docs.djangoproject.com/en/1.11/topics/testing/tools/#the-test-client)をもう一度見直さなければなりませんでした。これを明確にしていただきありがとうございます。 – arielnmz