-
-
Notifications
You must be signed in to change notification settings - Fork 527
-
Hello,
Usually I work with FastAPI but I recently started a new personal project with django-ninja because I needed an administration interface quickly.
I quickly realized that I was missing an equivalent to fastapi.Depends
which is a very efficient tool for factoring code.
I think it would also be a good entry point to facilitate library integration into django-ninja.
I wanted to know if I'd missed anything in the documentation or if there were strategies I didn't know about to achieve this.
I'm curious to hear your opinion on this.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 9 replies
-
@remimd what would you put to Depends ?
Beta Was this translation helpful? Give feedback.
All reactions
-
If you think it's unnecessary and I'm the only one who needs it, I'm not going to be picky.
Here are two simple examples I've found that could be useful in everyday life:
- Building objects with multiple data from the request:
from dataclasses import dataclass @dataclass class LimitAndOffset: limit: int offset: int def get_limit_and_offset(page: int = 1, per_page: int = 20) -> LimitAndOffset: return LimitAndOffset( limit=per_page, offset=per_page * (page - 1), )
- Retrieve resources directly:
async def get_cat(cat_id: int) -> Cat: return await Cat.objects.aget(id=cat_id)
You might think that this kind of thing is superfluous, but it reduces the number of parameters and processes to be performed in the view, which can make the code more readable.
Beta Was this translation helpful? Give feedback.
All reactions
-
well you can already achieve this with pydantic models:
limit/offset:
from ninja import Schema, Query class LimitAndOffset(Schema): limit: int offset: int @api.get("/filter") def events(request, filters: Query[LimitAndOffset]):
cat
class Cat(Schema): cat_id: int @model_validate def get_cat(...) @api.get("/filter") def events(request, cat: Query[Cat]): ...
I'm not trying to be picky but decision to avoid "dependency" injection they way it was done in fastapi was intentional (because simply starlet/fastpi lack infrastructure that was built on top of django that allowed to achive lot of thigns)
I'm not strongly against some Depend annotator - just trying to design a system so that all users have an easy way to pick the right tool for the job avoiding multiple options
Beta Was this translation helpful? Give feedback.
All reactions
-
Sorry, I don't know django-ninja well enough yet. Indeed, if it's already possible to do this kind of thing, Depends isn't really useful.
Beta Was this translation helpful? Give feedback.
All reactions
-
To come back to my initial need, I did a little research on how other people.
django-ninja-extra
has a controller system where dependency injection is done at the__init__
method level.anydi
patchesViewSignature
,Operation
andAsyncOperation
to achieve this.
But these two methods don't really appeal to me.
Beta Was this translation helpful? Give feedback.
All reactions
-
To conclude with Depends
, I don't have any further arguments to convince you. Maybe others in the future will have more concrete needs that support its adoption.
Beta Was this translation helpful? Give feedback.