You will have to create two classes, but the good news is that in most cases you do not have to enter attribute names several times. One thing I discovered if you use Flask, SQLAlchemy, and Marshmallow is that if you define some validation attributes in your Column definition, the Marshmallow schema will automatically select them and the validations provided in them. For instance:
import (your-database-object-from-flask-init) as db import (your-marshmallow-object-from-flask-init) as val class User(db.Model): name = db.Column(db.String(length=40), nullable=False) email = db.Column(db.String(length=100)) created_at = db.Column(db.DateTime) class UserSchema(val.ModelSchema): class Meta: model = User
In this example, if you take a data dictionary and put it in UserSchema (). Load (data), you will see errors if in this example the name does not exist or the name is longer than 40 characters, or the email address is longer than 100 characters. Any user checks, except that you still have to code in your circuit.
This also works if you created a model class as an extension of another model class, transferring its attributes. For example, if you want each class to create / modify information, you can put these attributes in the parent class of the model, and the child will inherit them along with the verification parameters. Marshmallow does not allow your parent model to have a schema, so I have no information about user checks there.
I know that you probably already completed your project, but I hope this helps other developers who are faced with this.
Corresponding pips list: Flask (1.0.2) flask-marshmallow (0.9.0) Flask-SQLAlchemy (2.3.2) marshmallow (2.18.0) marshmallow-sqlalchemy (0.15.0) SQLAlchemy (1.2.16)
source share