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

Model inheritance does not convert correctly #59

Open
VerzCar opened this issue Apr 13, 2021 · 1 comment
Open

Model inheritance does not convert correctly #59

VerzCar opened this issue Apr 13, 2021 · 1 comment

Comments

@VerzCar
Copy link

VerzCar commented Apr 13, 2021

Use Case Scenario

I've two pydantic models. Each in his own module (file). One Model, the TUser depends on the TAddress.

address_model.py

class TAddress(BaseModel):
    id: Optional[int]
    address: Optional[constr(max_length=100, strip_whitespace=True)]
    city: Optional[constr(max_length=80, strip_whitespace=True)]
    postal_code: Optional[constr(max_length=15, strip_whitespace=True)]
    country: Optional[constr(max_length=3)]

user_model.py

class TUser(BaseModel):
    id: UUID = None
    email: Optional[EmailStr]

    address: Optional[TAddress]

    is_active: Optional[bool]
    is_email_verified: Optional[bool]
    created_at: Optional[datetime.datetime]

If I use the TUser model know for my PydanticObjectType

class UserType(PydanticObjectType):
    class Meta:
        model = TUser

I get the following error message:

graphene_pydantic.converters.ConversionError: Don't know how to convert the Pydantic field ModelField(name='address', type=Optional[TAddress], required=False, default=None) (<class 'app.address.serializers.TAddress'>)

It seems like that there is a problem when using pydantic models from different modules that depends on each other.
What is the solution for this use case?

Any idea?

@mskoric
Copy link

mskoric commented Jun 5, 2021

You have to declare also TAddress as a PydanticObjectType class.

models are located in directory schemas


schemas/taddress.py
schemas/tuser.py

grapql_models are located in graphql_models

graphql_models/models.py

from app import schemas

class TAddress(PydanticObjectType):
           class Meta:
                   model = schemas. TAddress


class TUser(PydanticObjectType):
        class Meta:
            model = schemas.TUser`

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