101591 Views
79842 Views
45596 Views
44332 Views
40991 Views
33419 Views
Raspberry Pi Time machine
Now Ad-Free
Guiding Light
Sync Files on your Pis, with Syncthing
NextCloud
Buddy Jr.
Introduction to FreeCAD for Beginners
Building a Robot Arm with Raspberry Pi and PCA9685
Building User Authentication for Static Sites with FastAPI
Mastering Pydantic for Robust Data Validation
Mastering Markdown for Documentation with Jekyll
Introduction to Rust
KevsRobots Learning Platform
55% Percent Complete
By Kevin McAleer, 3 Minutes
In building our authentication system, the first step is to create models that define the structure of user data. These models ensure that the data is valid, consistent, and ready for storage in a database. We’ll use Pydantic for data validation and SQLAlchemy for Object-Relational Mapping (ORM) to interact with our database.
Pydantic uses Python type annotations to validate data. This ensures that the data conforms to specified formats before we process it or save it to our database.
Let’s define a simple user model with Pydantic to validate user registration data:
from pydantic import BaseModel, EmailStr class UserCreate(BaseModel): username: str email: EmailStr password: str
This model will validate that the username is a string, the email is a valid email address, and the password is also a string.
username
email
password
SQLAlchemy is an ORM library that allows us to interact with databases using Python classes and objects. It abstracts away SQL queries, making database operations more Pythonic and secure.
Now, let’s define an SQLAlchemy model for our users:
from sqlalchemy import Column, Integer, String from .database import Base class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True, index=True) username = Column(String, unique=True, index=True) email = Column(String, unique=True, index=True) hashed_password = Column(String)
Notice we’re storing hashed_password instead of password. It’s crucial never to store plain passwords in your database for security reasons.
hashed_password
While Pydantic models are great for input validation and serialization, SQLAlchemy models are used for database operations. In practice, you’ll often convert between these two model types.
For example, after validating user registration data with a Pydantic model, you’ll convert this data into an SQLAlchemy model before saving it to the database. Similarly, when fetching user data from the database, you’ll convert SQLAlchemy models into Pydantic models before sending them to clients.
You’ve learned how to define user models using Pydantic for validation and SQLAlchemy for database interaction. These models form the backbone of our user authentication system, ensuring data integrity and security. In the next lesson, we’ll implement user registration and login functionalities using these models.
Try adding additional fields to the UserCreate Pydantic model, such as first_name and last_name, and reflect on how these additions might be validated. Consider the types of validation that would be appropriate for these fields.
UserCreate
first_name
last_name
< Previous Next >