object has no attribute 'has_next' in pagination.PaginationSerializer

892 views
Skip to first unread message

Thomas Alisi

unread,
Feb 17, 2015, 8:41:33 AM2/17/15
to django-res...@googlegroups.com
hey

I have a problem that feels really n00b, but didn't manage to figure out what's happening here:

class SimpleStorylineSerializer(serializers.ModelSerializer):
   
class Meta:
        model
= Storyline
        fields
= ('text', 'language', 'featured', 'gallery', 'render_url')


class PaginatedStorylineSerializer(pagination.PaginationSerializer):
   
class Meta:
        object_serializer_class
= SimpleStorylineSerializer


class StorylineGallery(generics.ListAPIView):
    queryset
= Storyline.objects.filter(gallery=True)
    serializer_class
= PaginatedStorylineSerializer


throws an AttributeError: 'Storyline' object has no attribute 'has_next' in rest_framework/pagination.py in to_representation, line 17

using django 1.7.x and restframework 3.0.5

any idea? help greatly appreciated

Tom Christie

unread,
Feb 17, 2015, 8:45:38 AM2/17/15
to django-res...@googlegroups.com
Worth checking: any chance you've install master, rather than 3.0.5?

Thomas Alisi

unread,
Feb 17, 2015, 9:10:02 AM2/17/15
to django-res...@googlegroups.com
hey tom,

thanks for getting back so quickly!

I've just installed via: pip install git+ssh://g...@github.com:tomchristie/django-rest-framework.git@master

and unfortunately the problem is still there

if it's of any help, if I delete all instances from my model and call the endpoint, I get an empty list (i.e. a json array rather than an object)

Tom Christie

unread,
Feb 17, 2015, 9:17:18 AM2/17/15
to django-res...@googlegroups.com
No I was meaning make sure you're on 3.0.5, not on master.
Probably not related, just wanted to check, since there's a bunch of changes to pagination in the upcoming release (which will be 3.1.0 and is now merged into master)

Kevin Brown

unread,
Feb 17, 2015, 9:17:58 AM2/17/15
to django-res...@googlegroups.com
I believe the issue here is that you are setting a pagination serializer where DRF is expecting a standard serializer. As pagination serializers expect a `Page` object, they don't work well in the `serializer_class` on views.
I would try setting your `pagination_serializer_class` instead of `serializer_class`. This may require you to enable pagination if you have not done it already.

Kevin Brown

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thomas Alisi

unread,
Feb 17, 2015, 9:18:45 AM2/17/15
to django-res...@googlegroups.com
yes, I'm normally on 3.0.5. also tried with 3.0.4 and had the same issue, sorry :(

Thomas Alisi

unread,
Feb 17, 2015, 9:42:18 AM2/17/15
to django-res...@googlegroups.com
ok, I think I fixed it, thanks @kevin

trouble is I find the fix quite confusing, here my findings:

1. if I just set `pagination_serializer_class` as suggested by kevin 

class StorylineGallery(generics.ListAPIView):
    queryset
= Storyline.objects.filter(gallery=True)

    pagination_serializer_class
= PaginatedStorylineSerializer

it throws an assertion error (generics.py in get_serializer_class, line 189) 'StorylineGallery' should either include a `serializer_class` attribute, or override the `get_serializer_class()` method.

2. if I add a serializer_class, I then have a list of results that are not actually paginated

class StorylineGallery(generics.ListAPIView):
    queryset
= Storyline.objects.filter(gallery=True)

    serializer_class
= StorylineSerializer
    pagination_serializer_class
= PaginatedStorylineSerializer


3. if I then `enable` pagination, by adding an explicit parameter in my config, it starts working as expected

REST_FRAMEWORK = {
   
'PAGINATE_BY': 20,
}

so I guess it would be probably useful to add a bit of documentation in the pagination section of DRF and be more explicit about the requirements?

thanks a lot to both
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Tom Christie

unread,
Feb 17, 2015, 9:56:24 AM2/17/15
to django-res...@googlegroups.com
> so I guess it would be probably useful to add a bit of documentation in the pagination section of DRF and be more explicit about the requirements?

The pagination API kinda sucks, and is being refreshed in the upcoming 3.1.
Reply all
Reply to author
Forward
0 new messages