djantic icon indicating copy to clipboard operation
djantic copied to clipboard

Abstract class option for ModelSchema subclasses

Open mikulas-mrva opened this issue 2 years ago • 2 comments

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.

mikulas-mrva avatar Nov 23 '22 13:11 mikulas-mrva

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.

jordaneremieff avatar Nov 24 '22 00:11 jordaneremieff

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

mikulas-mrva avatar Nov 25 '22 18:11 mikulas-mrva