cosmicpython / code

Example application code for the python architecture book
Other
2.2k stars 956 forks source link

Pydantic for domain models? #91

Open drforse opened 1 month ago

drforse commented 1 month ago

Hello. I really liked this book, it gives a fresh (for me) perspective at coding a web application, and it looks fantastic, very well written, not too difficult to understand. I finished the part 1 a couple of hours ago, it turned my perspective upside down, and I decided to give my new knowledge a try, just writing a small web service. And I encountered a problem: if I want to use pydantic to write my domain models, when I apply the SQLAlchemy mapper on them, the models stop working, they simply throw error at initializing. I really love pydantic, but I also ain't sure it's worth implementing smth like in django appendix just because I wanna use pydantic insteadof dataclasses and just classes.

The error: AttributeError: 'User' object has no attribute 'pydantic_private'. Did you mean: 'pydantic_complete'?

hjwp commented 1 month ago

I like pydantic too! but i've never tried it with the sqla mapper. maybe something more manual is the only way to go...

hjwp commented 1 month ago

let me know if you find a good solution!

hjwp commented 1 month ago

I would say - don't be afraid of boilerplate like this. a dumb function that maps a sqlalchemy row-dictionary to a pydantic class in a really mechanical way might feel really tedious and verbose, but it really isn't that much of a maintenance burden.

hjwp commented 1 month ago

oh and also-by-the-way -- if you enjoyed the book, i don't suppose you'd have time to write a quick review online somewhere? on amazon or goodreads or wherever you like to put reviews? they really help 😊

drforse commented 1 month ago

oh and also-by-the-way -- if you enjoyed the book, i don't suppose you'd have time to write a quick review online somewhere? on amazon or goodreads or wherever you like to put reviews? they really help 😊

I'd be glad to leave a review everywhere I can, but I don't know where, could you give the urls?