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

Abstract class option for ModelSchema subclasses #62

Open
mikulas-mrva opened this issue Nov 23, 2022 · 2 comments
Open

Abstract class option for ModelSchema subclasses #62

mikulas-mrva opened this issue Nov 23, 2022 · 2 comments

Comments

@mikulas-mrva
Copy link

mikulas-mrva commented Nov 23, 2022

I am using Djantic in my project, and I would like to base a base class between ModelSchema and my individual model schemata, to implement a to_orm method directly on the ModelSchema, as in my complex case simply doing DjangoModel(**schema) doesn't work because of foreign keys and some other data transformations required. I could just implement it on each of them individually, but then mypy will be very unhappy.

I would like to be able to do the following:

class ModelSchemaToORM(ModelSchema):
    # either this, django model style
    class Meta:
        abstract = True

    # or this
    class Config:
        abstract = True

    async def to_orm(self):
        raise NotImplementedError

Currently the two versions above yield

pydantic.errors.ConfigError: 'Config' (Is `Config` class defined?)

and

pydantic.errors.ConfigError: type object 'Config' has no attribute 'model' (Is `Config.model` a valid Django model class?)

respectively.

Does it make sense to folks here to add an additional condition to ModelSchemaMetaclass to skip Config checks on abstract subclasses? It could either be in Meta or Config, I don't really mind as long as there is a way to put intermediate parents between my ModelSchemata and Djantic's ModelSchema.

@jordaneremieff
Copy link
Owner

Does it make sense to folks here to add an additional condition to ModelSchemaMetaclass to skip Config checks

I think this would be fine.

class MyAbstractSchema(ModelSchema):
    class Config:
        abstract = True

I don't have time to do it myself, but I'll review and merge a PR for this.

@mikulas-mrva
Copy link
Author

@jordaneremieff Please see my attempt at implementation in PR #63.
Please let me know if you have any comments.

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