Answer the question
In order to leave comments, you need to log in
Where in the code to place the data processing code?
I'm making a Python web application using Flask, SQLAlchemy and WTForms.
The task is to get some value from the user, process it and save it in the database.
How I do it - when entering a value in the form, it is checked for the possibility of input depending on the values already available in the database, then the function in the View (View as in MVC) slightly modifies the value depending on some external conditions and saves it to the database. The database table model has a function that, depending on the data already present, changes the stored value and it is stored in the database.
How to properly organize this process? To make it clear, safe and easy to maintain.
There is such an idea - At the form level, when entering data, perform all the necessary checks so that the value is already initially entered as needed. In the View, save to the database, and remove all additional methods from the model.
Answer the question
In order to leave comments, you need to log in
You now have some kind of porridge. In general, there are two approaches: fat models or fat controllers, it is better to continue to google on this topic.
In short: thick models mean that all business logic is hardwired into the model (generally speaking, the model is far from being equal to the display in the database), thick controllers mean the business logic is in the controllers. In the best houses in England, it is customary to use architecture with thick models due to a more flexible approach, and dragging some kind of business logic into a view is generally considered wild bad manners. Yes, and this is logical, given that models are a mapping of the subject area into an object model.
If it is even shorter, it is most correct to transfer all business logic and validation to the model.
Tired of reading on this topic. I came to this conclusion for myself:
Model:
- I describe the table in SQLAlchemy notation as a class.
- I make a static method using the @staticmethod decorator, I do some logic in it to get data from this table, I pass some parameters and give them the result accordingly.
- Using the @property decorator and the subsequent @bla-bla.setter decorator, I make a method that modifies the state of the class values, taking into account the existing data in the database, and also has the ability to change the intersecting data already in the database, and saves a new record.
Controller:
- Receives data from the database using the static method of the model, substitutes it in the View (I don't like the name View) and sends it to the user.
- After the user enters or changes data in the View, gets it and creates or changes it using the model's @bla-bla.setter method.
View:
- Displays and allows user input.
- Validates data before sending it to the controller. For example, it checks the correctness of entering e-mail. And the ability to set some value in certain ranges, taking into account those already existing in the database.
The question is, is this normal? Or is it worth checking with @staticmethod the possibility of setting the value in the model itself and in the view, just get True with it if it passes and accordingly issue a message to the user.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question