添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

So I decided to touch a new framework.
As mentioned above, I did not want to learn a new language because what I want to do is create things and programming is just a means to an end. (I was trying to get introduced to the Go language at one point, but gave it up due to lack of time).
After much research, I learned that a framework called FastAPI was coming along quite well.

Advantages of FastAPI

When I touched FastAPI, it was a revolution.

  • I can include typedefs in Python.
  • I was very happy to see this feature, as I had a hard time with DX dropping due to the lack of types in Django development at my internship.
  • Ability to output API documentation by default.
  • Genius.
  • It's very effective in the division of labor with the frontend.
  • fast .
  • cf. Python web framework performance comparison FastAPI settings from app.routers import auth_router , health_router , user_router from fastapi import FastAPI from fastapi.staticfiles import StaticFiles fastapi_app = FastAPI () # routers fastapi_app . include_router ( health_router , tags = [ "health" ], prefix = "/health" ) # to mount Django fastapi_app . mount ( "/django" , django_app ) fastapi_app . mount ( "/static" , StaticFiles ( directory = "static" ), name = "static" ) fastapi_app . mount ( "/media" , StaticFiles ( directory = "media" ), name = "media" ) Enter fullscreen mode Exit fullscreen mode
    class User(AbstractBaseUser, PermissionsMixin, BaseModelMixin):
        objects = UserManager()
        email = models.EmailField(_("email address"), unique=True)
        MIN_LENGTH_USERNAME = 1
        MAX_LENGTH_USERNAME = 20
        username = models.CharField(
            _("username"),
            max_length=MAX_LENGTH_USERNAME,
        # permissions
        is_active = models.BooleanField(default=True)
        is_admin = models.BooleanField(default=False)
        Enter fullscreen mode
        Exit fullscreen mode
    @user_router.get("/", response_model=ReadUserSchema)
    async def get(request: Request, current_user: User = Depends(get_current_user)) -> User:
        return UserAPI.get(request, current_user)
    @user_router.post(
        "/",
        response_model=ReadUserSchema,
    async def create(request: Request, schema: CreateUserSchema) -> User:
        return await UserAPI.create(request, schema)
        Enter fullscreen mode
        Exit fullscreen mode
    
    class UserAPI:
        @classmethod
        def get(cls, request: Request, current_user: User) -> User:
            return current_user
        @classmethod
        async def create(cls, request: Request, schema: CreateUserSchema) -> User:
            user = await User.objects.filter(email=schema.email).afirst()
            if user:
                raise HTTPException(status_code=400, detail="Email already registered")
            schema.password = hash_password(schema.password)
            return await sync_to_async(User.objects.create)(**schema.dict())
        Enter fullscreen mode
        Exit fullscreen mode
    

    I am happy to build my ideal style. Since it is in the template repository, I hope you will use it.
    As I am new to FastAPI, I am sure there are some weird things I am doing. Please send me Issue, PR.

    https://www.stavros.io/posts/fastapi-with-django
    https://qiita.com/Ningensei848/items/ac72ff6edf4d887cdcc1

    My encounter with this article was the beginning of this project.

    https://kigawas.me/posts/integrate-fastapi-and-django-orm/
    https://github.com/kigawas/fastapi-django

    Most helpful.

    https://nsikakimoh.com/learn/django-and-fastapi-combo-tutorials

    I found it while writing this article.
    I think it differs from this project in that it uses wsgi to run Django and does not support async for the Django ORM.

    Built on Forem — the open source software that powers DEV and other inclusive communities.

    Made with love and Ruby on Rails. DEV Community © 2016 - 2025.

  •