Ninja doesn’t have strict requirements on where you define things. There’s no expectation about file names. In previous lessons, you’ve seen a single way of organizing your API. in this lesson, you’ll cover tools in Ninja for handling more complex structures.
More Complex Organization
00:00 In the previous lesson, I showed you a complete set of CRUD operations. In this lesson, I’ll show you a mix of organizational techniques you might use as you build larger interfaces with Ninja. These examples aren’t in the sample code.
You can do this in Ninja through the use of the
api_operation decorator. It takes a list of HTTP operation names indicating what the view should accept. This can be particularly helpful if you need to write an API endpoint that uses an HTTP operation that doesn’t have a corresponding decorator, for example the HEAD or OPTIONS operations.
01:31 Everything you’ve seen with the router can be done with the API object itself. If you’re writing a smaller application or you’re organizing all of your API calls inside of a single Django app, you don’t need the extra indirection of a router.
If you’re not using a router and you’ve declared the API inside of an app, you just import that API object inside of your main
urls file and use it inside of the path declaration like you’ve seen so far, no difference there.
02:33 If you need to make a breaking change in your API, you change the version number in the path as well. This allows you to support both the old version and the new version for some amount of time.
This is why the default namespace is
api-1.0.0. Like you saw on the previous lesson, the version number tag itself doesn’t impact the path. If you’re going to use version info in the path, you do it manually, like I’ve done here, when you register the path.
03:49 And of course, you can have different routers for each one of your different Ninja objects. This is no different than what you’ve seen so far. And just to reiterate, as I mentioned, the version number changes the namespace,
Become a Member to join the conversation.