kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Fastapi open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Fastapi

0.75.2

0.0.6

Revert backward incompatible changes

0.0.11

fastapi

0.75.2

sqlmodel

0.0.6

databases

Revert backward incompatible changes

opyrator

0.0.11

fastapi-users

Popular Libraries in Fastapi

Trending New libraries in Fastapi

Top Authors in Fastapi

1

13 Libraries

1097

2

11 Libraries

396

3

9 Libraries

290

4

7 Libraries

93

5

6 Libraries

54057

6

5 Libraries

1071

7

4 Libraries

980

8

4 Libraries

1889

9

4 Libraries

4386

10

4 Libraries

425

1

13 Libraries

1097

2

11 Libraries

396

3

9 Libraries

290

4

7 Libraries

93

5

6 Libraries

54057

6

5 Libraries

1071

7

4 Libraries

980

8

4 Libraries

1889

9

4 Libraries

4386

10

4 Libraries

425

Trending Kits in Fastapi

No Trending Kits are available at this moment for Fastapi

Trending Discussions on Fastapi

    FastAPI - GET request results in typeerror (value is not a valid dict)
    Conditional call of a FastApi Model
    fastapi (starlette) RedirectResponse redirect to post instead get method
    uvicorn [fastapi] python run both HTTP and HTTPS
    how to render a json from a dataframe in fastAPI
    FastAPI - Pydantic - Value Error Raises Internal Server Error
    Kill a python subprocess that does not return
    "422 Unprocessable Entity" error when making POST request with both attributes and key using FastAPI
    FastAPI responding slowly when calling through other Python app, but fast in cURL
    How to apply transaction logic in FastAPI RealWorld example app?

QUESTION

FastAPI - GET request results in typeerror (value is not a valid dict)

Asked 2022-Mar-23 at 22:19

this is my database schema.

enter image description here

I defined my Schema like this:

from pydantic import BaseModel

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6

This is my model:

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13

In a crud.py I define a get_attributes method.

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13def get_attributes(db: Session, skip: int = 0, limit: int = 100):
14    return db.query(models.Userattribute).offset(skip).limit(limit).all()
15

This is my GET endpoint:

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13def get_attributes(db: Session, skip: int = 0, limit: int = 100):
14    return db.query(models.Userattribute).offset(skip).limit(limit).all()
15@app.get("/attributes/", response_model=List[schemas.Userattribute])
16def read_attributes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
17    users = crud.get_attributes(db, skip=skip, limit=limit)
18    print(users)
19    return users
20

The connection to the database seems to work, but a problem is the datatype:

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13def get_attributes(db: Session, skip: int = 0, limit: int = 100):
14    return db.query(models.Userattribute).offset(skip).limit(limit).all()
15@app.get("/attributes/", response_model=List[schemas.Userattribute])
16def read_attributes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
17    users = crud.get_attributes(db, skip=skip, limit=limit)
18    print(users)
19    return users
20pydantic.error_wrappers.ValidationError: 7 validation errors for Userattribute
21response -> 0
22  value is not a valid dict (type=type_error.dict)
23response -> 1
24  value is not a valid dict (type=type_error.dict)
25response -> 2
26  value is not a valid dict (type=type_error.dict)
27response -> 3
28  value is not a valid dict (type=type_error.dict)
29response -> 4
30  value is not a valid dict (type=type_error.dict)
31response -> 5
32  value is not a valid dict (type=type_error.dict)
33response -> 6
34  value is not a valid dict (type=type_error.dict)
35

Why does FASTApi expect a dictionary here? I don´t really understand it, since I am not able to even print the response. How can I fix this?

ANSWER

Answered 2022-Mar-23 at 22:19

SQLAlchemy does not return a dictionary, which is what pydantic expects by default. You can configure your model to also support loading from standard orm parameters (i.e. attributes on the object instead of dictionary lookups):

copy icondownload icon

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13def get_attributes(db: Session, skip: int = 0, limit: int = 100):
14    return db.query(models.Userattribute).offset(skip).limit(limit).all()
15@app.get("/attributes/", response_model=List[schemas.Userattribute])
16def read_attributes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
17    users = crud.get_attributes(db, skip=skip, limit=limit)
18    print(users)
19    return users
20pydantic.error_wrappers.ValidationError: 7 validation errors for Userattribute
21response -> 0
22  value is not a valid dict (type=type_error.dict)
23response -> 1
24  value is not a valid dict (type=type_error.dict)
25response -> 2
26  value is not a valid dict (type=type_error.dict)
27response -> 3
28  value is not a valid dict (type=type_error.dict)
29response -> 4
30  value is not a valid dict (type=type_error.dict)
31response -> 5
32  value is not a valid dict (type=type_error.dict)
33response -> 6
34  value is not a valid dict (type=type_error.dict)
35class Userattribute(BaseModel):
36    name: str
37    value: str
38    user_id: str
39    id: str
40
41    class Config:
42        orm_mode = True
43

You can also attach a debugger right before the call to return to see what's being returned.

Since this answer has become slightly popular, I'd like to also mention that you can make orm_mode = True the default for your schema classes by having a common parent class that inherits from BaseModel:

copy icondownload icon

1class Userattribute(BaseModel):
2    name: str
3    value: str
4    user_id: str
5    id: str
6class Userattribute(Base):
7    __tablename__ = "user_attribute"
8
9    name = Column(String)
10    value = Column(String)
11    user_id = Column(String)
12    id = Column(String, primary_key=True, index=True)
13def get_attributes(db: Session, skip: int = 0, limit: int = 100):
14    return db.query(models.Userattribute).offset(skip).limit(limit).all()
15@app.get("/attributes/", response_model=List[schemas.Userattribute])
16def read_attributes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
17    users = crud.get_attributes(db, skip=skip, limit=limit)
18    print(users)
19    return users
20pydantic.error_wrappers.ValidationError: 7 validation errors for Userattribute
21response -> 0
22  value is not a valid dict (type=type_error.dict)
23response -> 1
24  value is not a valid dict (type=type_error.dict)
25response -> 2
26  value is not a valid dict (type=type_error.dict)
27response -> 3
28  value is not a valid dict (type=type_error.dict)
29response -> 4
30  value is not a valid dict (type=type_error.dict)
31response -> 5
32  value is not a valid dict (type=type_error.dict)
33response -> 6
34  value is not a valid dict (type=type_error.dict)
35class Userattribute(BaseModel):
36    name: str
37    value: str
38    user_id: str
39    id: str
40
41    class Config:
42        orm_mode = True
43class OurBaseModel(BaseModel):
44    class Config:
45        orm_mode = True
46
47
48class Userattribute(OurBaseModel):
49    name: str
50    value: str
51    user_id: str
52    id: str
53

This is useful if you want to support orm_mode for most of your classes (and for those where you don't, inherit from the regular BaseModel).

Source https://stackoverflow.com/questions/69504352