app-generator / django-api-generator

API Generator for Django - Open-Source Library | AppSeed
https://app-generator.dev/docs/developer-tools/api-generator.html
MIT License
37 stars 3 forks source link

Adding Response Headers #1

Open marqpdx opened 1 year ago

marqpdx commented 1 year ago

I am wanting to use this with React Admin, which requires an 'X-Total-Count' response header. i was able to hard-code it (see below) but i am wondering if there are thoughts about having a config for additional resp. headers we might add?

Thanks!

            theData = [BookClubSerializer(instance=obj).data for obj in BookClub.objects.all()]
            resp = Response(theData, status=200)
            resp.headers['X-Total-Count'] = 2
            resp.headers['Access-Control-Expose-Headers'] = 'X-Total-Count'
            return resp
app-generator commented 1 year ago

Hello @marqpdx

Ty for using the library. Where exactly this specific code is saved?

AppSeed Support

marqpdx commented 1 year ago

hi @app-generator :)

I have a model named BookClub and i ran the generate step and i added my code to the generated api/views.py file, specifically to the if not pk: section of the get() method. My new code replaced this:

            return Response({
                'data': [BookClubSerializer(instance=obj).data for obj in BookClub.objects.all()],
                'success': True
            }, status=HTTPStatus.OK)

PS, in my code i wouldn't hardcode the value of '2' for the resp.headers['X-Total-Count']. i am also investigating if there is possibly a decorator to add, or some other way to globally add headers w/o interfering w/ the generated code.

app-generator commented 1 year ago

@Mortrest, can you take a look at this case? Ty