"Cannot resolve keyword 'created' into field" on new DRF list view

Viewed 633

I made a new list view in my Django REST Framework app:

class ColumnView(ListCreateAPIView):
    queryset = Column.objects.all()
    serializer_class = ColumnSerializer
    permission_classes = [IsAuthenticatedOrReadOnly]

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

When I try to access it, I get the following error:

FieldError at /my/new/endpoint

Cannot resolve keyword 'created' into field. Choices are: _order, fields, from, my, model

There's no created field anywhere in sight - not in the ColumnSerializer, not in the Column Django model, nowhere. The stacktrace is really opaque, too - my app doesn't appear anywhere in it. What's going on?

1 Answers

Did you enable cursor pagination in your settings?

REST_FRAMEWORK = {
    ...
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.CursorPagination',
    ...
}

Be sure to read the docs on CursorPagination carefully:

Details and limitations

Proper use of cursor based pagination requires a little attention to detail. You'll need to think about what ordering you want the scheme to be applied against. The default is to order by "-created". This assumes that there must be a 'created' timestamp field on the model instances, and will present a "timeline" style paginated view, with the most recently added items first.

In other words, you can't have all three of these conditions:

  • Using cursor pagination
  • ...without specifying an ordering
  • ...on a model without a created field
Related