Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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 >