pydantic UserWarning: Valid config keys have changed in V2

355 Views Asked by At

I'm getting an error related to migrating the Pydantic version. Code made in v1, and pydantic installed in v2.

enter image description here

enter image description here

I try replace the class Config for the model_config, but i don't had success and the error continued the same. I tried too replace the field for de annotation, but i dont have success.

the codes where i do using pydantic:

config:

from pydantic import BaseConfig


class Settings(BaseConfig):
    DATABASE_URL: str
    MONGO_INITDB_DATABASE: str

    ACCESS_TOKEN_EXPIRES_IN: int
    REFRESH_TOKEN_EXPIRES_IN: int
    JWT_ALGORITHM: str

    CLIENT_ORIGIN: str

    JWT_PRIVATE_KEY: str
    JWT_PUBLIC_KEY: str

    class Config:
        env_file = "./env"


settings = Settings()

model (here i dont know how i could add the prop json_encoders in model config):

from typing import List, Annotated

from bson import ObjectId
from pydantic import BaseModel, Field, BaseConfig, ConfigDict


class OID(str):
    @classmethod
    def __get_validators__(cls):
        yield cls.validate

    @classmethod
    def validate(cls, v):
        try:
            return ObjectId(str(v))
        except Exception:
            raise ValueError("not a valid ObjectId")


class MyBaseModel(BaseModel):
    def __hash__(self):
        return hash((type(self),) + tuple(self.__dict__.values()))


class Projects(MyBaseModel):
    # model_config = ConfigDict(
    #     arbitrary_types_allowed=True,
    #     populate_by_name=True,
    # )

    title: str = Field(description='titulo', alias='title')
    imagePath: str = Field(description='path image', alias='imagePath')
    url_project: str = Field(default='', description='path image', alias='url')

    class Config(BaseConfig):
        arbitrary_types_allowed = True
        json_encoders = {
            ObjectId: lambda oid: str(oid)
        }


class User(BaseModel):
    #
    # model_config = ConfigDict(
    #     arbitrary_types_allowed=True,
    #     populate_by_name=True,
    # )

    # id_user: ObjectId = Field(description='user id', alias='_id')
    name: str
    login: str
    projects: List[Projects] = Field(alias='projects')

    class Config(BaseConfig):
        arbitrary_types_allowed = True
        json_encoders = {
            ObjectId: lambda oid: str(oid)
        }


class CreateUserSchema(User):
    password: str
    passwordConfirm: str
    verified: bool = False


class LoginUserSchema(BaseModel):
    name: str
    password: str


class UserResponseSchema(User):
    id: str
    pass


class UserResponse(BaseModel):
    status: str
    user: UserResponseSchema
0

There are 0 best solutions below