encode / django-rest-framework

Web APIs for Django. 🎸
https://www.django-rest-framework.org
Other
28.24k stars 6.82k forks source link

Add pagination to RawSQL from custom model object #6569

Closed diegohdez90 closed 5 years ago

diegohdez90 commented 5 years ago

Checklist

Steps to reproduce

I have a custom RawSQL with inner join.

I create a custom class object Category (this is not created in models.py):

class Category(object):
    def __init__(self, **kwargs):
        for field in ('id', 'budget', 'concept', <my other fields>):
            setattr(self, field, kwargs.get(field, None))

then I create a serializer class

class CategorySerializer(serializers.Serializer):
    # attributes

    def create(self, validated_data):
        return Category(id=None, **validated_data)

    def update(self, instance, validated_data):
        for field, value in validated_data.itens():
            setattr(instance, field, value)
        return instance

The next step is create the view:

class CategoryViewSet(viewsets.ViewSet):
    serializer_class = CategorySerializer

    def list(self, request):
        queryset = Budget.objects.raw("""
            <my query>
        """) 

        serializer = CategorySerializer(lqueryset, many=True)
        return Response(serializer.data)

also add it into routes:

router.register('category', CategoryViewSet, base_name='Category')

Now, I get my json response correctly.

I based in this tutorial in how implement a custom model for serializer only:

Expected behavior

But I have a doubt in how to implement a pagination in my RawSQL, someone ha ve any idea how write the code correctly to add pagination

Actual behavior

I get my json :

[{"id":1,"budget":"2019","concept":{"id":3,"created":"2019-03-27T23:16:08.488838Z","modified":"2019-03-27T23:16:08.488838Z","concept":"Operaciones"},"funding_source":{"id":2,"created":"2019-03-27T21:17:53.328323Z","modified":"2019-03-27T21:17:53.328323Z","name":"Fuente 2","funding_code":"F002"},"status":"APPROVED","authorization_status":"AUTH","amount":"63000.00","created":"2019-03-28T19:09:41.438873Z","modified":"2019-03-28T23:38:27.352999Z","authorized_by":{"username":"director","email":"","area":"DIRECTOR"},"created_by":{"username":"finanzas","email":"","area":"FINANCE"},"amount_budgetbudgetitem":"4500.00","amount_available":"58500.00"},{"id":2,"budget":"2019","concept":{"id":2,"created":"2019-03-27T23:15:45.724183Z","modified":"2019-03-27T23:15:45.724183Z","concept":"Mantenimiento"},"funding_source":{"id":4,"created":"2019-03-27T21:18:17.572434Z","modified":"2019-03-27T21:18:17.572434Z","name":"Fuente 4","funding_code":"F004"},"status":"APPROVED","authorization_status":"AUTH","amount":"630000.00","created":"2019-03-28T19:16:15.273980Z","modified":"2019-03-28T23:38:37.358911Z","authorized_by":{"username":"director","email":"","area":"DIRECTOR"},"created_by":{"username":"finanzas","email":"","area":"FINANCE"},"amount_budgetbudgetitem":"30000.00","amount_available":"600000.00"}]

without pagination class, how can I implement a pagination with RawSQL?

thanks.

xordoquy commented 5 years ago

The discussion group is the best place to take this discussion and other usage questions. Thanks!