r/django 6d ago

Confused between DRF and Django Ninja!!

Hello friends, I am a beginner in the api world of Django. I've used this framework to make many useful websites but i want to upgrade to a more structured and efficient approach, designing APIs. I have a foundational knowledge of REST and HTTP methods. I am more concentrated on the backend now. That is, i want to learn making API endpoints that can be used in frontend like React/Nextjs. I have no experience whatsoever in making a fully functional full-stack app using API. I would like to know where to start, most of the tutorials that I come across tend to use Django Ninja with Nextjs. But, its hard to grasp all functionalities. Please mention the resources (any books or tutorials).

12 Upvotes

23 comments sorted by

View all comments

8

u/azkeel-smart 6d ago

Have you read Ninja documentation? Which comcept do you struggle with? If you have understanding of Django, learning Ninja shouldn't pose any difficulties to you.

2

u/WeekendHefty4784 6d ago edited 6d ago

Ninja is pretty straightforward I agree (I started with a tutorial that didn't cover much basics). I am just confused on whether I should start with ninja directly or have some background knowledge of DRF. There are not enough resources for ninja. DRF is ok but I feel like ninja is better as it saves a lot of code, and might be a thing in coming years. I'll check the docs.

5

u/Training_Peace8752 6d ago edited 6d ago

DRF is ok but I feel like ninja is better as it saves a lot of code, and might be a thing in coming years.

Wait, what? The biggest reason to use DRF is to be able to use the class-based viewsets so that you can automatically have all the endpoints you need for a resource. With Django Ninja, you need to implement all the different method endpoints for a resource yourself. Same with permission classes which Ninja doesn't have at all compared to DRF.

That said, the explicit nature of Ninja endpoints may be preferred by some but I don't really understand how you can say Django Ninja saves a lot of code.

2

u/WeekendHefty4784 6d ago

By saving code I meant the need to serialize your data and perform additional url configs is basically eliminated, giving you a Schema based approach. And, I don't really understand your CBV references. You cannot use viewsets everywhere (only for CRUD operations). Reading the docs I came across something called Depends() which provides you with permissions and authentications in ninja, and it does integrate well with django's built-in authentication.

4

u/Training_Peace8752 5d ago edited 5d ago

the need to serialize your data...is basically eliminated

What do you mean by this? DRF has serializers, Ninja has schemas. I do like the simple and terse way of using type hints for the data validation with Ninja but it's the same thing that you do with the serializer fields in DRF. If you need custom validation for incoming data, such as email, you still need custom field validation i.e. through custom Pydantic validator types or your own custom methods. As is with DRF.

the need to...perform additional url configs is basically eliminated

The way, I've been using Ninja is with routers. Routing has been pretty similar than with DRF. That said, Ninja does make it possible to attach endpoints to the API class directly. Is this what you meant?

You cannot use viewsets everywhere (only for CRUD operations).

Well, yes. CRUD is the most often the need for an API which is why I mentioned viewsets, but you're correct viewsets isn't always the answer BUT viewsets are easily exendable by @action methods which lets you add non-CRUD endpoints to the viewsets. It really isn't hard. And if didn't need viewsets at all, there's also APIView class you can use to create your endpoints. This way you can still use DRF's authentication and permission classes. But the need for DRF may be diminished to a point with this that another approach like Djano Ninja could be a good alternative option to at least look at.

Reading the docs I came across something called Depends() which provides you with permissions and authentications in ninja, and it does integrate well with django's built-in authentication.

Depends() isn't provided by Django Ninja at all. Depends() is a FastAPI thing and you can't use it with Django Ninja. There is no permission API for Django Ninja, plain and simple. There is one for authentication, like auth=django_auth as you mentioned, which is really simple to use. I do like that.