django-ninja-extra
django-ninja-extra copied to clipboard
Feat: ModelController
New Feature
ModelController
- Create a new ModelControllerBase type to easily manage django models. Something similar to DRF ModelViewSet
from ninja_extra import api_controller, ModelControllerBase, http_post
from .models import Post
@api_controller('/posts')
class PostController(ModelControllerBase):
model = Post
retrieve_schema = schemas.PostSchema
create_schema = schemas.CreatePostSchema
update_schema = schemas.PatchPostSchema
@http_post('/', response={201: schemas.PostSchema}
def create_item(self, data: schemas.CreatePostSchema):
# this will override default create_item endpoint
return data.dict()
api = NinjaExtraAPI(title='ModelController Test')
api.register_controllers(PostController)

Nice feature. Interestingly I wrote a similar thing, it will automatically scan Django-apps and create Nestjs-CRUD like APIs for each models. It will create schema based on Meta configurations.
@api_controller("events", permissions=[BaseApiPermission])
class EventControllerAPI(BaseAdminAPIController):
def __init__(self, service: EventService):
super().__init__(service)
self.service = service
class Meta:
model = Event
exclude = ["update_time", "create_time"]
@http_get("/crud_get_objs_all", summary="crud_get_objs_all unittest")
@paginate
async def get_objs_all_with_filters(self, request, maximum: int = None, **filters):
await self.service.dummy_biz_logics()
await self.service.demo_action(data="running /get_objs_all_with_filters")
return await self.service.get_objs(maximum, **filters)
I spent some time and put those codes together, still in early stage, comments and advices are highly appreciated.
https://github.com/freemindcore/django-api-framework
- Auto CRUD API generation for all django models, configurable
- Domain/Service/Controller base structure for better code organization
- Base Permission class and more to come
Thanks
WOW, this is awesome. I have checked out the https://github.com/freemindcore/django-api-framework. What you have build was the initial idea for django-ninja-extra before I branched of to build ellar. If you enjoy NestJS, then you will definitely enjoy ellar.
For this feature, I intend to go with DRF Serializer approach,
having Schema fields defined in Meta in fields, exclude, read_only and write_only attributes. That way, CRUD schema will be fully defined.
I will still look deep into your project or even contribute to it. I love the idea. Good job!!
Great! Looking forward to your advices or contributions.
Now I get to understand why you started ellar project. I love ellar too. It is a huge effort writing it from scratch on your own. Python is becoming more and more popular in server-side web development, I am sure Ellar sort of new framework will become a super star in the future!
Please keep up the good work!
Codecov Report
Merging #27 (f2f2923) into master (790eede) will decrease coverage by
1.04%. Report is 5 commits behind head on master. The diff coverage is94.07%.
:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.
@@ Coverage Diff @@
## master #27 +/- ##
===========================================
- Coverage 100.00% 98.96% -1.04%
===========================================
Files 45 51 +6
Lines 1923 2322 +399
===========================================
+ Hits 1923 2298 +375
- Misses 0 24 +24
| Files | Coverage Δ | |
|---|---|---|
| ninja_extra/__init__.py | 100.00% <ø> (ø) |
|
| ninja_extra/controllers/__init__.py | 100.00% <100.00%> (ø) |
|
| ninja_extra/controllers/base.py | 100.00% <100.00%> (ø) |
|
| ninja_extra/controllers/model/__init__.py | 100.00% <100.00%> (ø) |
|
| ninja_extra/controllers/model/interfaces.py | 100.00% <100.00%> (ø) |
|
| ninja_extra/controllers/model/service.py | 100.00% <100.00%> (ø) |
|
| ninja_extra/controllers/route/__init__.py | 100.00% <100.00%> (ø) |
|
| ninja_extra/schemas/__init__.py | 100.00% <100.00%> (ø) |
|
| ninja_extra/controllers/model/builder.py | 98.52% <98.52%> (ø) |
|
| ninja_extra/controllers/model/endpoints.py | 95.12% <95.12%> (ø) |
|
| ... and 1 more |