Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An equivalent for fastapi.Depends #1459

remimd started this conversation in Ideas
May 7, 2025 · 1 comments · 9 replies
Discussion options

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.

You must be logged in to vote

Replies: 1 comment 9 replies

Comment options

@remimd what would you put to Depends ?

You must be logged in to vote
9 replies
Comment options

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.

Comment options

@remimd

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

Comment options

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.

Comment options

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 patches ViewSignature, Operation and AsyncOperation to achieve this.

But these two methods don't really appeal to me.

Comment options

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /