4 ms·
If your needs are simple enough, built-in Django function-based views will do. When you outgrow those, DRF's complexity is often warranted.
by Nextgrid 5y ago
If your needs are simple enough, built-in Django function-based views will do. When you outgrow those, DRF's complexity is often warranted.
- throwaway_4ever 5y agoBut is DRFs complexity worth it compared to Ninja?
- dangerbird2 5y agoDRF was about as good as it got for automatic schema generation and data validation before python static type hints made things like pydantic possible. It also sets up good defaults for stuff like query parameter-based filtering, pagination, and resource relationships (it supports HATOAS by default).
- Nextgrid 5y agoI'd argue that Python type hints are actually a step back. Sure, they may work 80% of the time, but there are times where you need the extra flexibility offered by DRF serializers. Those can go way farther than the basic "map these JSON data types to this Python representation" which would then be difficult to represent using basic Pydantic-style hints (you'd have to the rest in procedural Python within your endpoint, which can't easily be reused when it comes to function-based views, where as DRF abstracts that away within the serializer which not only can be reused, but itself can be composed of multiple classes/mixins).
- leowoo91 5y agoDRF isn't necessarily complex, you can still use its generic views.
- Nextgrid 5y agoMy argument about the "complexity" of DRF is that you need to use its serializers (frankly if you're just returning raw dicts then you're already not far off from built-in, generic Django function-based views) which I guess some people may consider complex especially if the data you're representing doesn't originate from models.
- zmmmmm 5y ago> If your needs are simple enough, built-in Django function-based views will do This is the bit I find not really true. My needs are simple (10-20 basic get/post/put/delete APIs, many at the business logic layer than true REST) but I still would really like the OpenAPI docs, auto-generated schema definitions, etc etc. If I can get that with just some simple decorators on my existing views and pydantic models for the inputs and outputs .... that would be awesome.
- Nextgrid 5y agoThat's something you can get with DRF pretty much out of the box as long as you use its routers, viewsets and serializers. DRF serializers are different than just Python type-hints, but not more complex - it's just different syntax.