pydantic: Getting error: 'Post' object has no attribute 'model_dump' (MRE attached in comment).
Initial Checks
- I confirm that I’m using Pydantic V2 installed directly from the
main
branch, or equivalent
Description
I am using:
pydantic==2.0.2
pydantic-extra-types==2.0.0
pydantic-settings==2.0.1
pydantic_core==2.1.2
with fastapi.
I am just copying and pasting the model from pydantic docs on which I am using .model_dump()
method. It is throwinf that attribute error mentioned above.
Example Code
from fastapi import FastAPI
from pydantic import BaseModel
# Pydantic Post model
class Post(BaseModel):
title: str
desc: str
draft: bool = False
rating: int | None = None
app = FastAPI()
@app.post('/create-post')
async def create_post(post: Post):
print(post.model_dump())
return post
Python, Pydantic & OS Version
pydantic version: 2.0.2
pydantic-core version: 2.1.2 release build profile
install path: /Users/jon/Documents/code/psnal/fastapi-tut/venv/lib/python3.11/site-packages/pydantic
python version: 3.11.0 (v3.11.0:deaf509e8f, Oct 24 2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)]
platform: macOS-13.4.1-arm64-arm-64bit
optional deps. installed: ['email-validator', 'typing-extensions']
Selected Assignee: @Kludex
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 1
- Comments: 16 (7 by maintainers)
It works fine.
You are not.
https://stackoverflow.com/help/minimal-reproducible-example
Please create a GitHub repository with a
requirements.txt
so I can reproduce your issue.