SoFunction
Updated on 2025-04-08

Python calls Orator ORM for database operations

Orator ORM is a feature-rich and flexible Python ORM (Object Relational Mapping) library designed to simplify database operations. It supports a variety of databases (including SQLite, PostgreSQL, and MySQL) and provides a simple and intuitive API for easy data manipulation by developers. Orator ORM combines the features of query builders, models, migrations, etc., and is suitable for medium to large applications.

Orator ORM Main Features

Powerful query builder: Orator ORM provides a powerful query builder that supports various SQL operations such as SELECT, INSERT, UPDATE, DELETE, as well as complex joins, sorts and aggregations.

Automated database migration: Orator ORM provides built-in database migration tools to help developers make table structure changes in applications.

Easy to Extend: Orator ORM offers a variety of extensions and customization options that can be easily integrated with other Python libraries or frameworks.

Flexible model definition: Orator ORM allows developers to use Python classes to define database tables, and supports field definitions, relationships (such as one-to-many, many-to-many), etc.

Good documentation support: The documentation of Orator ORM is very detailed and has rich examples for developers to get started and use.

Install

You can install Orator ORM via pip:

pip install orator

Example of usage

Here is a basic example of an Orator ORM covering how to define a model, perform database operations, and perform queries.

1. Define the database connection:

Before you start using Orator ORM, you need to configure the database connection.

from orator import DatabaseManager, Model

# Configure database connectionsconfig = {
    'sqlite': {
        'driver': 'sqlite',
        'database': ''
    }
}

db = DatabaseManager(config)
Model.set_connection_resolver(db)

2. Define the model

The model class needs to inherit the Model class of Orator, and fields can be defined by the field type provided by Orator.

from orator import Model

class User(Model):
    __fillable__ = ['name', 'email', 'age']  # Fields that can be assigned in batches

3. Database operations (add, check, modify, delete)

Orator ORM provides a simple API for common database operations.

# Create a new useruser = ({'name': 'John', 'email': 'john@', 'age': 30})

# Query a single useruser = (1)  # Search by primary keyprint(, )

# Query all usersusers = ()
for user in users:
    print()

# Update user datauser = (1)
({'age': 31})

# Delete the user()

4. Use the query builder:

Orator ORM provides a powerful query builder that supports SQL operation chain calls.

# Query: Get all users older than 30users = ('age', '>', 30).get()
for user in users:
    print()

# Sort: Ascending order of ageusers = User.order_by('age').get()

# Aggregation: Calculate the average age of all usersaverage_age = ('age')
print("Average age:", average_age)

5. Database migration:

Orator ORM provides migration capabilities to easily manage database structure changes.

# Create a migration fileorator migrate:make create_users_table

# Run the migrationorator migrate:run

6. Relationship (model association):

Orator ORM supports common database relationships such as one-to-many and many-to-many.

from orator import Model

class Post(Model):
    __fillable__ = ['title', 'body']
    
    # Define one-to-many relationship    def comments(self):
        return self.has_many(Comment)

class Comment(Model):
    __fillable__ = ['post_id', 'body']

Summarize

Orator ORM is a powerful and flexible Python ORM for medium to large projects. It combines query builder, database migration and model support, allowing developers to easily perform complex database operations. Its ease of use and intuitive API make Orator ORM the preferred ORM library for many Python developers.

If you need an ORM that is both feature-rich and efficient in operating databases, and want to avoid excessive complexity, Orator ORM is a very worthwhile choice.

This is the article about Python calling Orator ORM for database operations. For more related Python database operations, please search for my previous article or continue browsing the following related articles.