-
Notifications
You must be signed in to change notification settings - Fork 0
Models and Schemas
The models and schemas are stored in the models folder
Models are made with sqlalchemy(read for more info), the only change from sqlalchemy models is to create a method:
- changes
The changes method receives a json as a parameter:
Json key values are assigned to the columns of the database:
Take any action on the data, In the example the password is encrypted
The constructor receives as a parameter a json and call the changes method; this to avoid doing repetitive code
The changes method will serve to insert data in json format both to create a new record (init method) and to modify it (changes method)
The schemes are made with marshmallow (read for more)
In this case the id is not defined in the schema because it is autoincrement,the other fields are defined depending on the type of data they will store, additionally marshmallow provides more varied data types to achieve better validations.
password will not show in json output for client, that's why load_only is set to True
In the meta class, the fields displayed to the client are defined in the fields variable, but if a field was previously defined as load only, it will not be displayed.
This process will do for each model you create!
In this example we have 5 models, which are inherited in classes inside database_init.py