@pytest.mark.django_db
def test_apiview_get_single_m2m(rf):
 view = UserM2MAPIView.as_view()
 user = UserFactory.create(username='foo')
 request = create_api_request(rf)
 response = view(request, id=user.id)
 response_data = json.loads(response.content)
 assert (response_data['alt_src_langs'] == [])
 user.alt_src_langs.add(LanguageDBFactory(code='alt1'))
 user.alt_src_langs.add(LanguageDBFactory(code='alt2'))
 request = create_api_request(rf)
 response = view(request, id=user.id)
 response_data = json.loads(response.content)
 assert response_data['alt_src_langs']
 assert (response_data['alt_src_langs'] == list((str(l) for l in user.alt_src_langs.values_list('pk', flat=True))))

@ pytest . mark . django_db def test_apiview_get_single_m2m ( rf ) : view = UserM2MAPIView . as_view ( ) user = UserFactory . create ( username = 'foo' ) request = create_api_request ( rf ) response = view ( request , id = user . id ) response_data = json . loads ( response . content ) assert ( response_data [ 'alt_src_langs' ] == [ ] ) user . alt_src_langs . add ( LanguageDBFactory ( code = 'alt1' ) ) user . alt_src_langs . add ( LanguageDBFactory ( code = 'alt2' ) ) request = create_api_request ( rf ) response = view ( request , id = user . id ) response_data = json . loads ( response . content ) assert response_data [ 'alt_src_langs' ] assert ( response_data [ 'alt_src_langs' ] == list ( ( str ( l ) for l in user . alt_src_langs . values_list ( 'pk' , flat = True ) ) ) )
tests retrieving a single object with an m2m field using the api .