Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation error for User #72

Closed
escapenate opened this issue Oct 1, 2019 · 4 comments
Closed

Validation error for User #72

escapenate opened this issue Oct 1, 2019 · 4 comments

Comments

@escapenate
Copy link

Hi

I used the cookiecutter to create this project locally on macOS 10.14.6 and ran it without making any changes.

I ran into the following issue with the API (and consequently the frontend):

backend_1        | pydantic.error_wrappers.ValidationError: 1 validation error for User
backend_1        | response -> 0
backend_1        |   value is not a valid dict (type=type_error.dict)

This example was with /api/v1/users/me but it occurred with all APIs returning the User model.

I was able to successfully return the user in raised exceptions all the way through the backend processing, so it seems that the validation error is being raised from db_session_middleware .

Adding the orm_mode property to the UserBase model seemed to fix the issue. ie.

class UserBase(BaseModel):
    email: Optional[str] = None
    is_active: Optional[bool] = True
    is_superuser: Optional[bool] = False
    full_name: Optional[str] = None

    class Config:
        orm_mode = True

I'm not sure whether this is a bug, or something to do with my configuration or OS - but this was a completely vanilla build, so perhaps I'm not the only person to encounter this issue.

@ltieman ltieman mentioned this issue Oct 20, 2019
@borland667
Copy link

borland667 commented Nov 6, 2019

I had the same issue and it looks like there was a change in behavior with the latest pydantic and now it doesn't take the db models as is.

I ended up extending the BaseModel from pydantic like this:

 from pydantic import BaseModel, BaseConfig

class BasicModel(BaseModel):
    class Config(BaseConfig):
        orm_mode = True

And using BasicModel in all my pydantic models.

br3ndonland added a commit to br3ndonland/full-stack-fastapi-postgresql that referenced this issue Dec 6, 2019
See GitHub PR fastapi#43
fastapi#43
See GitHub issue fastapi#72
fastapi#72

When viewing the user profile, name and email weren't showing up.

Docker Compose logs showed a pydantic error:
https://pydantic-docs.helpmanual.io/

```
backend_1        | pydantic.error_wrappers.ValidationError: 1 validation
                   error for User
backend_1        | response -> 0
backend_1        |   value is not a valid dict (type=type_error.dict)
```

Pydantic orm_mode is needed. This commit will update the database models
to use pydantic orm_mode, with cleaner syntax than the fix suggested in
issue fastapi#72 (by adding class Config directly to the base class).
@tiangolo
Copy link
Member

Thanks for the report and the discussion here, everyone!

This should be fixed in master now. 🎉

@escapenate may we close this issue now?

@escapenate
Copy link
Author

Works perfectly - thanks @tiangolo and @br3ndonland

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@tiangolo @borland667 @escapenate and others