django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

Custom baseclass for ModelSchema without a model specified

Open jrocketfingers opened this issue 1 year ago • 1 comments

I'd like to create a baseclass that enforces alias_generator = to_camel, but I can't seem to dance around the ModelSchema metaclass expectation of meta.model being specified.

The naïve approach I've tried was:

class BaseModelSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        alias_generator = to_camel

In addition to that, I tried a few ways to replace the metaclass with the ABCMeta, but it didn't seem to work.

Has anyone done something like this? Is it foolish to do so? Maybe I should just focus on creating a Meta class to inherit?

jrocketfingers avatar Dec 10 '23 02:12 jrocketfingers

Curently I am doing it like this:

class CamelCase(Schema):
    model_config = ConfigDict(alias_generator=to_camel_case, populate_by_name=True)
class ColorSchema(CamelCase, ModelSchema):
    class Meta:
        model = Color
        fields = "__all__"

But it would be cleaner if I could just create subclass of ModelSchema with same config as CamelCase class.

jozekuhar avatar Jan 03 '24 12:01 jozekuhar