django-rest-framework-bulk icon indicating copy to clipboard operation
django-rest-framework-bulk copied to clipboard

Exception "'BulkListSerializerObject' is not iterable" when using API browser

Open typerlc opened this issue 9 years ago • 8 comments

Posting to a BulkModelViewSet using the DRF api browser causes the following exception to be thrown. NB: the data has already been written to the db successfully before the exception happens:

TypeError at /usage_records/
'BulkListSerializer' object is not iterable
Request Method: POST
Request URL:    http://127.0.0.1:8000/usage_records/
Django Version: 1.7.4
Exception Type: TypeError
Exception Value:    
'BulkListSerializer' object is not iterable
Exception Location: /Users/richardc/virtualenvs/rating_sytstem/lib/python2.7/site-packages/django/template/defaulttags.py in render, line 161
Python Executable:  /Users/richardc/virtualenvs/rating_sytstem/bin/python
Python Version: 2.7.8
Python Path:    
['/Volumes/Data/git/rating-system/rating_system_site',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python27.zip',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python2.7',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python2.7/plat-darwin',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python2.7/plat-mac',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python2.7/lib-tk',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python2.7/lib-old',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python2.7/lib-dynload',
 '/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/richardc/virtualenvs/rating_sytstem/lib/python2.7/site-packages']
Server time:    Thu, 12 Mar 2015 13:33:11 +1100

The line of code that throws the exception is in the django template library, and it is doing:

values=list(values)

The values object is my serializer object:

Usage_Record_Serializer(context={u'view': <rating_system.views.rest.Usage_Record_ViewSet object>, u'request': <rest_framework.request.Request object>, u'format': None}, data=[{u'event_description': u'Test Event', u'event_value': 122, u'tags': u'PURCHAGES, VISA', u'reference_id': 20, u'external_service': 1, u'external_system': 2, u'external_service_key': u'Etx', u'event_date_time': u'2015-03-02T00:10:13.869285Z'}, {u'event_description': u'Test Event', u'event_value': 122, u'tags': u'PURCHAGES, VISA', u'reference_id': 21, u'external_service': 1, u'external_system': 2, u'external_service_key': u'Etx', u'event_date_time': u'2015-03-02T00:10:13.869285Z'}], many=True)

If I just browse to http://127.0.0.1:8000/usage_records/ again, the template renders correctly, with these new records included.

The basic code is:

class Usage_Record_Serializer(BulkSerializerMixin, serializers.ModelSerializer):
    class Meta(object):
        model = Usage_Record
        list_serializer_class = BulkListSerializer
        read_only_fields = ('id', 'upload_date_time',)

class Usage_Record_ViewSet(BulkModelViewSet):
    queryset = Usage_Record.objects.all()
    serializer_class = Usage_Record_Serializer
    permission_classes = (permissions.IsAuthenticated,)

bulk_router.register(r'usage_records', rest.Usage_Record_ViewSet)

If I use postman or curl, no error is thrown ... so this does seem to be some bad interaction with the DRF api browser.

Any thoughts on why this happens?

Thanks, Richard

typerlc avatar Mar 12 '15 02:03 typerlc

Sorry forgot to add versions:

Python: 2.7.8 Django: 1.7.4 DRF: 3.1.0 DRF-bulk: 0.2

typerlc avatar Mar 12 '15 04:03 typerlc

This is known issue, even with DRF2. DRF-bulk for some reason was not compatible with the API browser. Ideally this should not happen. It looks like you already went through some debugging so PRs are welcome!

miki725 avatar Mar 14 '15 16:03 miki725

can confirm: Django==1.7.1 DRF==2.4.4 DRF-bulk==0.2

arahayrabedian avatar Mar 18 '15 11:03 arahayrabedian

confirmed on: Django==1.8 DRF==3.1.1 DRF-bulk==0.2

chubz avatar Apr 22 '15 11:04 chubz

any update, guys? Django==1.6.11 DRF==3.1.1 DRF-bulk==0.2

strelnikov avatar May 14 '15 11:05 strelnikov

Looks like if you just add this to the object it works fine...

def __iter__(self):
    return self

def next(self):
    raise StopIteration

liquidpele avatar Jul 15 '15 19:07 liquidpele

@liquidpele which object? if that fixes the issue, do you mind opening a PR?

miki725 avatar Jul 15 '15 19:07 miki725

Any updates on this issue?

django==1.9.9 DRF==3.2.4 DRF-bulk==0.2.1

markliederbach avatar Sep 12 '16 15:09 markliederbach