question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issues in APIRouter.__init__() got an unexpected keyword argument 'prefix' using FastApI

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from ..import models, schemas
from fastapi import FastAPI, Response, status, HTTPException, Depends, APIRouter 
from sqlalchemy.orm import Session
from .. database import get_db
from typing import List

router = APIRouter(
    prefix="/posts",
    tags=['Posts'])


@router.get("/", response_model=List[schemas.Post])
def get_posts(db: Session = Depends(get_db)):

    posts = db.query(models.Post).all()
    # cursor.execute(""" SELECT * FROM posts """)
    # posts = cursor.fetchall()
    return posts

@router.post("/", status_code= status.HTTP_201_CREATED, response_model=schemas.Post)
def create_posts(post: schemas.PostCreate, db: Session = Depends(get_db)):
    # cursor.execute(""" INSERT INTO posts (title, content, published) VALUES(%s , %s, %s) RETURNING *""",
    # (post.title, post.content, post.published))

    # new_post = cursor.fetchone()
    # conn.commit()
    # print(**post.dict())
    new_post= models.Post(**post.dict())
    db.add(new_post)
    db.commit()
    db.refresh(new_post)
    return new_post

@router.get("/{id}", response_model=schemas.Post)
def get_post(id: int, response: Response, db: Session = Depends(get_db)):
    # cursor.execute(""" SELECT * FROM posts WHERE id = %s """ ,(str(id)))
    # post= cursor.fetchone()
    post = db.query(models.Post).filter(models.Post.id ==id).first()
    print(post)
    if not post:
        raise HTTPException(status_code = status.HTTP_404_NOT_FOUND, 
                                        detail = f"post with id : {id} was not found")
    return post


@router.delete("/{id}", status_code= status.HTTP_204_NO_CONTENT)
def delete_post(id: int, db: Session = Depends(get_db)):
    # cursor.execute (""" DELETE FROM posts WHERE id = %s RETURNING * """, (str(id)))
    # deleted_post= cursor.fetchone()
    # conn.commit()

    post_query = db.query(models.Post).filter(models.Post.id ==id)
    if post_query.first == None:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail = f"post with {id} does not exit")

    post_query.delete(synchronize_session=False)
    db.commit()

    return Response(status_code=status.HTTP_204_NO_CONTENT)


@router.put("/{id}", response_model=schemas.Post)
def update_post(id: int, updated_post: schemas.PostCreate, db: Session = Depends(get_db)):
    # cursor.execute("""UPDATE posts SET title = %s, content=%s, published=%s WHERE id = %s RETURNING* """, (post.title, post.content, post.published, str(id)),)
    # updated_post = cursor.fetchone()
    # conn.commit()

    post_query = db.query(models.Post).filter(models.Post.id ==id)

    post = post_query.first()
    if post == None:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail = f"post with {id} does not exit")
    
    post_query.update(updated_post.dict(), synchronize_session=False)
    db.commit()
    return post_query.first()

Description

File “C:\Users\OneDrive\Desktop\API_Development\fastapi.\app\main.py”, line 13, in <module> from app.routers import post, user File “C:\Users\OneDrive\Desktop\API_Development\fastapi.\app\routers\post.py”, line 7, in <module> router = APIRouter( TypeError: APIRouter.init() got an unexpected keyword argument ‘prefix’

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.52.0

Python Version

Python 3.10.1

Additional Context

Please help

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
rafsafcommented, Jan 15, 2022

Please look at release notes. Your version is kind of 2 years behind. It’s 2/3 of whole fastapi lifetime (I’m on phone, this is estimated).

I’m aware of few security issues and breaking changes 😉

For me, upgrade is required.

But, if it’s not possible, your only chance is to search through source code of your version and find a solution, probably you can add prefix in app.include_router.

0reactions
adhamsalamacommented, Apr 23, 2022

@Nidit2512 it’s “FastAPI”, not “FASTAPI”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generic Inline fails (unexpected keyword argument `prefix`)
I got a type exception saying, "init() got an unexpected keyword argument 'prefix'". I found out that it was caused by a GenericTabularInline, ......
Read more >
django - __init__() got an unexpected keyword argument 'prefix'
I have two forms and they have init and when i remove it from form then prefix work's correct. My forms.py class ClientUserCreateForm(forms....
Read more >
tiangolo/fastapi - Gitter
from dataclasses import dataclass, asdict from fastapi import FastAPI, ... I get an error TypeError: build() got an unexpected keyword argument 'stream'.
Read more >
Bigger Applications - Multiple Files - FastAPI
As it is inside a Python package (a directory with a file __init__.py ), it is a ... We are going to include...
Read more >
Extra Models - FastAPI
Continuing with the previous example, it will be common to have more than one related model. ... And then adding the extra keyword...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found