Skip to content

Models and Schemas

CRAG666 edited this page Jun 23, 2020 · 8 revisions

Models

The models and schemas are stored in the models folder

Models are made with sqlalchemy(read for more info), there are only two change in sqlalchemy models. They are inheriting from Standard and creating a method called changes:

  • changes method
  • inherit from standard

The models would look like this:

model

Structure

In this line db (sqlalchemy instance), ma (marshmallow instance) and Standard (Class) are imported

imports

Model class must inherit from db.Model(read about sqlalchemy) and Standard

Modelclas

Create fields (sqlalchemy)

Fields

The changes method receives a json as a parameter:

changes

Json key values ​​are assigned to the columns of the database:

fields

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

cons

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)

Another model example

other

Standard Class

The Standard class is found in the file _int_.py

Standard

The class helps us to save, update or delete data, all the models created must inherit from it for its correct operation, below it is shown how to use it in different contexts.

Schemas

The schemes are made with marshmallow (read for more)

The schemas would look like this:

schema

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.

fields

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.

meta

Declare model for creation

The finished model should look something like this

complete model

In the database_int.py file, create a class that inherits from your model's class

database_init

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

5 class

Save, update and delete data

Use in any url context, the examples use the model shown above, but you can use it with any model that follows the structure already stated

Save

Save

An object is created from the model, the model is passed as a parameter a json with all the data expected by the model

ins

The save method returns true if the data was successfully saved

save

Update

update

the changes method receives as a parameter a json with the new values ​​for the register(put method)

changes

Save the changes (the save method is already explained in the previous chapter)

save

Delete

delete

The delete method returns true if the data was successfully deleted

mthdelete

Note: You may want to validate the json, this is covered in the Routes chapter. For data serialization use marshmallow.