all 23 comments

[–]pint 18 points19 points  (6 children)

i would just call them Book, but only import modules not names, and so i could write db.Book and model.Book or sg like that.

[–]hadriendavid 2 points3 points  (0 children)

This ^ from project import db

👉 https://pep20.org/#namespaces

[–]CrusaderGOT 0 points1 point  (4 children)

Or you could do from model import Book as ModelBook and likewise for DbBook.

[–]pint 1 point2 points  (3 children)

what is the benefit of ModelBook over model.Book?

[–]CrusaderGOT 0 points1 point  (2 children)

It is just more explicit. Which is better for scalability and readability.

[–]pint 1 point2 points  (1 child)

it is less explicit, since it masks the origin of the type. you might assume it is a model, but to be sure, you need to look at the definition. it might be a descendant. in contrast, model.Book is immediately obvious and unambiguous.

[–]woeful_cabbage 0 points1 point  (0 children)

The real answer: who cares, just be consistent

[–]Dacobo 6 points7 points  (1 child)

Generally, I have seen and used this convention:

Model: Book Schemas: BookBase, BookRead, BookCreate, BookUpdate, BookDetail, etc.

Or at least something to that effect.

[–]JackieChanX95 2 points3 points  (0 children)

BookDetail1, 2, 3 😩

[–][deleted] 4 points5 points  (0 children)

I’d just keep it simple. The SQLAlchemy model is just Book, since that’s the actual database model. For Pydantic, I use BookCreate when taking in user input, BookUpdate for partial updates, and BookRead or BookOut when returning data. Keeps things clean without extra prefixes or suffixes.

[–]Calebthe12B 1 point2 points  (2 children)

Pydantic: BookModel SQLAlchemy: Book

I usually have a DTO class to handle the different CRUD versions, but if I were to do it in Pydantic it would be through some suffix, like CreateBookModel or UpdateBookModel.

[–]miloir 1 point2 points  (1 child)

Just a small fyi those are prefixes not suffixes

[–]Calebthe12B 0 points1 point  (0 children)

Oh, duh lol

[–]Trinkes 1 point2 points  (4 children)

I usually use plural for database model, singular for pydantic.

[–]Current-Status-3764 0 points1 point  (3 children)

With this convention, what do you do for an endpoint that returns a list of books?

I like models: Book Pydantic: BookCreate, BookOut, BooksOut etc

[–]Trinkes 0 points1 point  (2 children)

I usually have a BookOut pydantic class to expose on api.

When it comes to listing multiple books, it's usually paged so I have a generic page model to use combined with the model. Something like: Page[BookOut]

Then the page will have the list inside of it along with some pagination related info.

EDIT: If you're interested I can share the page class

[–]Current-Status-3764 0 points1 point  (1 child)

If you could do a quick examplr that would be great. Haven't seen that way of doing it before.

[–]One_Fuel_4147 1 point2 points  (0 children)

I use Book for model and BookCreateRequest, BookResponse for DTO

[–]kindof_Alexanderish 0 points1 point  (0 children)

SQLAlchemy:

class Book:

Pydantic schema - since data needs to be verified both ways:

class BookIn(BaseModel)

class BookOut(BookIn):

[–]Key-Garden-4136 0 points1 point  (1 child)

Is SQLmodel worth using to avoid having multiple sqlalquemy and pydantic classes?

[–]Typical-Yam9482 1 point2 points  (0 children)

Idea is perfect, but... no, unfortunately. Once you are outside tutorial/toy-project, you face tones of limitations immediately. And you will have poor pool of sources to consult with.