FastAPI Create Authentication & Post Request - 44

2023. 1. 7. 17:01·Develop/FastAPI
728x90
SMALL
users 테이블에 유저 데이터를 저장할 POST API를 생성

 

1. 인증 관련 데이터를 다루기 위한 파이썬 파일 생성

TodoApp 하위에 파일을 만든다.

 

2. Users 클래스에 대한 BaseModel(유효성 검사) 생성 및 POST API 생성 

※id는 primary key로 따로 설정 x

from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
import models

class CreateUser(BaseModel):
    username: str
    email: Optional[str]
    first_name: str
    last_name: str
    password: str
    
app = FastAPI()

@app.post("/create/user")
async def create_new_user(create_user: CreateUser):
    create_user_model = models.Users()
    create_user_model.email = create_user.email
    create_user_model.username = create_user.username
    create_user_model.first_name = create_user.first_name
    create_user_model.last_name = create_user.last_name
    create_user_model.hashed_password = create_user.password
    create_user_model.is_active = True

    return create_user_model

 

3. Swagger 확인

 

유저 데이터를 생성한다.

 

728x90
SMALL
저작자표시 비영리 변경금지 (새창열림)

'Develop > FastAPI' 카테고리의 다른 글

FastAPI 데이터베이스에 유저 데이터 저장하기 - 46  (0) 2023.01.07
FastAPI bcrypt 비밀번호 암호화 - 45  (0) 2023.01.07
FastAPI Create Users Table / Create Foreign Key - 43  (0) 2023.01.07
FastAPI Database Relationship / Foreign Key / Query- 42  (0) 2023.01.07
FastAPI Delete Request를 통한 데이터 삭제 - 41  (0) 2023.01.04
'Develop/FastAPI' 카테고리의 다른 글
  • FastAPI 데이터베이스에 유저 데이터 저장하기 - 46
  • FastAPI bcrypt 비밀번호 암호화 - 45
  • FastAPI Create Users Table / Create Foreign Key - 43
  • FastAPI Database Relationship / Foreign Key / Query- 42
동석해요
동석해요
공부하고 싶은게 많은, 사소한 IT 지식들 공유
    250x250
  • 동석해요
    개발로 자기계발
    동석해요
  • 전체
    오늘
    어제
    • 분류 전체보기 (226)
      • Develop (126)
        • 기초지식 (12)
        • FastAPI (102)
        • Django (11)
      • Database & Data (62)
        • 기초지식 (16)
        • MySQL (29)
        • PostgreSQL (8)
        • 데이터 분석 (9)
      • 인공지능 (11)
        • PyTorch (9)
      • Cloud (8)
        • AWS (4)
        • GCP (2)
      • 버그처리 (14)
      • 회고 & 일상 (5)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
동석해요
FastAPI Create Authentication & Post Request - 44
상단으로

티스토리툴바