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

Provide a way to customize the model Config class #285

Closed
naturalethic opened this issue Jul 24, 2021 · 5 comments
Closed

Provide a way to customize the model Config class #285

naturalethic opened this issue Jul 24, 2021 · 5 comments
Labels
enhancement New feature or request

Comments

@naturalethic
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I would like to customize the model config, specifically to specify allow_mutation = False.

Describe the solution you'd like
If a Config class is defined in the model, use it instead of get_pydantic_base_orm_config/

Describe alternatives you've considered
The only solution I've found so far is to subclass ormar's ModelMetaclass and override the new method.

@naturalethic naturalethic added the enhancement New feature or request label Jul 24, 2021
@collerek
Copy link
Owner

Yeah I had it on mind for quite some time.

You still can edit the config after class declaration as it's stored in __config__ attribute, so after your class declaration you can set:

class Model(ormar.Model):
    ...

Model.__config__.allow_mutation = False

But allowing to overwrite the config in class declaration should be the preferred way.

@naturalethic
Copy link
Contributor Author

Oh great, that will work for me for now, thanks. I was just typing up a patch, so I'll keep that below, I'm sure you're aware of this possible solution.

In ormar.models.metaclass around line 560, replace:

        attrs["Config"] = get_pydantic_base_orm_config()

with:

        if not "Config" in attrs:
            attrs["Config"] = get_pydantic_base_orm_config()

@collerek
Copy link
Owner

Yep, if you can issue a PR with this solution that would be welcomed too! :)

@naturalethic
Copy link
Contributor Author

#286

@collerek
Copy link
Owner

collerek commented Aug 6, 2021

Released in 0.10.16 - thanks again! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants