D
D
domanskiy2021-02-09 18:44:18
Flask
domanskiy, 2021-02-09 18:44:18

Methods or functions for working with the database through SQLAlchemy?

I'm making an API in Flask.
There was a question: how it is better to work with a DB through ORM SQLAlchemy?
1. Write functions in the routes
db.query...
db.add(data)
db.commit

You can, as an option, move the functions to a separate services file

2. Write methods in the alchemy table model classes.

Both options are working.
But why are methods in classes better?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2021-02-09
@kshnkvn

You write common requests that can be repeated many times in the code in the model class so that you don’t duplicate the code later, everything else is in place, that is, in the routes themselves. Putting 1-2 lines of code into some separate file, which are used 1 time in 1 place - such an idea.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question