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

AttributeError: 'FieldInfo' object has no attribute 'name' #190

Closed
0x587 opened this issue Aug 9, 2023 · 3 comments
Closed

AttributeError: 'FieldInfo' object has no attribute 'name' #190

0x587 opened this issue Aug 9, 2023 · 3 comments

Comments

@0x587
Copy link

0x587 commented Aug 9, 2023

I got this problem when I just try the demo!
Python3.11.4

from pydantic import BaseModel
from fastapi import FastAPI
from fastapi_crudrouter import MemoryCRUDRouter as CRUDRouter


class Potato(BaseModel):
    id: int
    color: str
    mass: float
    pass


app = FastAPI()
app.include_router(CRUDRouter(schema=Potato))

import uvicorn

uvicorn.run(app)

image
image

@SaladBreaker
Copy link

Hello, I encountered the same problem and by downgrading pydentic to 1.10.6 it got solved. I think this is only a temporary solution and somebody should definitely look into it.

@SaladBreaker
Copy link

Duplicate with: #189

@0x587
Copy link
Author

0x587 commented Aug 17, 2023

Thanks for your great advice.
I found that solving this problem only requires downgrading pydentic to a version below 2.0. Use the command

pip install "pydantic==1.*"

The problem occurs when pydantic2.x modifies

BaseModel.__fields__

I provide a feasible solution here, just modify the schema_factoryfunction on line 22 of core/_utils.py.

# for handle pydantic 2.x migration
from pydantic import __version__ as pydantic_version

if int(pydantic_version.split('.')[0]) >= 2:
    # pydantic 2.x
    fields = {
        fk: (fv.annotation, ...)
        for fk,fv in schema_cls.model_fields.items()
        if fk != pk_field_name
    }
else:
    # pydantic 1.x
    fields = {
        f.name: (f.type_, ...)
        for f in schema_cls.__fields__.values()
        if f.name != pk_field_name
    }

I'll submit a Pull Request to fix this later.

@0x587 0x587 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 24, 2023
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

2 participants