djantic icon indicating copy to clipboard operation
djantic copied to clipboard

Pydantic 2 Support

Open etimberg opened this issue 1 year ago • 7 comments

I was testing out djantic with pydantic 2.4.2 and ran into an error caused by an import that was removed from pydantic.

  File "/usr/local/lib/python3.11/site-packages/djantic/main.py", line 12, in <module>
    from pydantic import BaseModel, ConfigError, create_model
ImportError: cannot import name 'ConfigError' from 'pydantic' (/usr/local/lib/python3.11/site-packages/pydantic/__init__.py)

The root cause of this appears to be that ConfigError was removed from pydantic during the v1 -> v2 transition as mention in the migration guide

etimberg avatar Oct 12 '23 15:10 etimberg

Please can this issue be solved? Also the Pydantic’s 'from_orm' method has been replaced with the 'model_validate' with a proper config for the model

giulioindev avatar Oct 22 '23 11:10 giulioindev

I tried patching the ConfigError issue, but immediately ran into another import problem. ModelMetaClass, used here has been made private

etimberg avatar Oct 22 '23 23:10 etimberg

Now should be _model_construction.ModelMetaclass

giulioindev avatar Oct 23 '23 00:10 giulioindev

Yup, but I also read https://github.com/pydantic/pydantic/issues/6381 where the maintainers of pydantic mention that they want to be able to change it without it being a breaking change. If possible, it might be good to refactor djantic to avoid using it directly.

etimberg avatar Oct 23 '23 13:10 etimberg

+1 for this. I'll be doing migration from Django to FastAPI and switching to V2 of Pydantic could be great

wjurkowlaniec avatar Nov 05 '23 17:11 wjurkowlaniec

Is there someone looking at this issue already? +1 for this.

joepreludian avatar Dec 13 '23 00:12 joepreludian

Djantic now supports pydantic 2 in a fork that I made https://github.com/jordaneremieff/djantic/issues/79

jonathan-s avatar Mar 15 '24 12:03 jonathan-s