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)

Most upvoted comments

According to your pyproject.toml you are on 0.89.1

That replit was just to quickly send some code and I used a template where the latest version was not insalled so now I sent my actual repository: Iqueal/fastapi-learn where I am using all the approprate versions which I mentiond earlier.

It works fine.

I am sure that I am using fastApi version 0.100.0

You are not.

BTW what is MRE?

https://stackoverflow.com/help/minimal-reproducible-example

Please create a GitHub repository with a requirements.txt so I can reproduce your issue.