How to configure nested ApiControllers #162
-
In django-ninja you can define I am curious if the same functionality is available the django-ninja-extra
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You can combine controllers to achieve that but not in a nested fashion. from ninja_extra import NinjaExtraAPI, api_controller, http_get, ControllerBase
api = NinjaExtraAPI()
@api_controller("/modela")
class ModelAController(ControllerBase):
@http_get("/")
def index(self):
return {"message": "List all ModelA objects"}
@http_get("/{id}/")
def get_model_a_by_id(self):
return {"message": "Get single ModelA object"}
@api_controller("/modela/{a_id}/modelb")
class ModelAModelBController(ControllerBase):
@http_get("/")
def index(self, a_id: int):
return {"message": f"List all ModelB objects related to ModelA by {a_id}"}
@http_get("/{b_id}/")
def get_model_b_by_id(self, a_id: int, b_id: int):
return {"message": f"Get single ModelB={b_id} object related to ModelA={a_id}"}
api.register_controllers(ModelAController, ModelAModelBController) In api_controller, if you pass a path parameter as a prefix as shown in |
Beta Was this translation helpful? Give feedback.
I will still look into adding nested controller but for now, I think this works