A Beginner's Guide to Tortoise ORM and PostgreSQL
155 views
13 slides
Dec 26, 2024
Slide 1 of 13
1
2
3
4
5
6
7
8
9
10
11
12
13
About This Presentation
Unlock the power of Python with Tortoise ORM and PostgreSQL in this comprehensive beginner's guide! Whether you're new to Object-Relational Mappers (ORMs) or want to integrate PostgreSQL with your Python projects, this video is your go-to resource.
In this step-by-step tutorial, you'll ...
Unlock the power of Python with Tortoise ORM and PostgreSQL in this comprehensive beginner's guide! Whether you're new to Object-Relational Mappers (ORMs) or want to integrate PostgreSQL with your Python projects, this video is your go-to resource.
In this step-by-step tutorial, you'll learn:
How to install and set up Tortoise ORM.
Connecting your Python application to a PostgreSQL database.
Writing models and performing CRUD operations seamlessly.
Best practices for database management and performance optimization.
Perfect for developers looking to streamline their database workflows or switch from traditional SQL queries to a modern ORM approach.
Boost your Python development skills today and take your projects to the next level!
š Donāt forget to like, comment, and subscribe for more tutorials on Python, ORMs, and backend development.
Size: 780.74 KB
Language: en
Added: Dec 26, 2024
Slides: 13 pages
Slide Content
Samuel Folasayo
Master Tortoise ORM &
PostgreSQL: Your Ultimate
Beginner's Guide
Simplifying Python/PostgreSQL Database Integration
Joe Nyirenda
Learning Objectives
āUnderstand the basics of Tortoise ORM and PostgreSQL
āLearn how to setup and conļ¬gure your environment
āDeļ¬ne models and perform CRUD operations
āQuery data and handle relationships effectively
āBuild eļ¬cient and scalable Python applications with Tortoise ORM and
PostgreSQL
What is Tortoise ORM?
A lightweight Object Relational Mapper (ORM) for Python.
Built with asyncio support for high-performance applications.
Simple syntax and seamless integration with Python's async features.
Key Features:
Asynchronous execution
Schema generation
Support for multiple backends, including PostgreSQL.
Why PostgreSQL?
Open-source, powerful, and reliable relational database system.
Features advanced data types and robust query capabilities.
Ideal for scalable and complex applications.
Why Use Tortoise ORM with PostgreSQL?
Simpliļ¬es database interactions.
Enhances developer productivity.
Combines PostgreSQL's power with Python's simplicity.
Setting Up the Environment
Prerequisites:
Python 3.7+
PostgreSQL installed on your system.
Deļ¬ning Models
Example Model:
The code below deļ¬nes a Tortoise ORM model for a User with ļ¬elds for id, name, email,
and created_at, representing a user entity in a database.
from tortoise.models import Model
from tortoise import fields
class User(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=50)
email = fields.CharField(max_length=100, unique=True)
created_at = fields.DatetimeField( auto_now_add=True)
Querying Data
Example Queries:
Filter by Condition:
users = await User.filter(name__icontains="John")
users = await User.all().order_by( "-created_at")
Order by Field:
Paginate:
users = await User.all().offset( 10).limit(10)
Relationships
Deļ¬ning Relationships:
class Post(Model):
id = fields.IntField(pk=True)
title =
fields.CharField(max_length=100)
content = fields.TextField()
author =
fields.ForeignKeyField( "models.User",
related_name="posts")
Fetching Related Data:
user = await
User.get(id=1).prefetch_related( "po
sts")
for post in user.posts:
print(post.title)
Explanation:
In this example, we are modeling a one-to-many relationship between the User
and Post models using Foreign Key in Tortoise ORM.
One-to-many relationship: This means that a single user can have many posts,
but each post is associated with exactly one user.
Putting It All Together
Example: Complete Workļ¬ow
async def main():
await init()
# Create user and post
user = await User.create(name="Alice", email="[email protected]" )
post = await Post.create(title="My First Post", content="Hello World!",
author=user)
# Query and display
users = await User.all().prefetch_related( "posts")
for user in users:
print(f"{user.name} wrote {len(user.posts)} posts.")
await main()
Results
Conclusion
Tortoise ORM simpliļ¬es database interactions in Python
PostgreSQL provides a robust backend for scalable applications
Combined, they enable fast and eļ¬cient development