Skip to main content

The User model and schema

  • Set metadata above
  • Start writing!
  • Create start folder
  • Create end folder
  • Create per-file diff between end and start (use "Compare Folders")

Just as we did with items, stores, and tags, let's create two classes for our users:

  • The SQLAlchemy model, to interact with the database.
  • The marshmallow schema, to deserialize data from clients and serialize it back to return data.

The User SQLAlchemy model

models/user.py
from db import db


class UserModel(db.Model):
__tablename__ = "users"

id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(80), unique=True, nullable=False)

Let's also add this class to models/__init__.py so it can then be imported by app.py:

models/__init__.py
from models.user import UserModel
from models.item import ItemModel
from models.tag import TagModel
from models.store import StoreModel
from models.item_tags import ItemsTags

The User marshmallow schema

schemas.py
class UserSchema(Schema):
id = fields.Int(dump_only=True)
username = fields.Str(required=True)
password = fields.Str(required=True, load_only=True)