Skip to content

Integration with Tortoise ORM. #2399

Closed Answered by 0x1618
0x1618 asked this question in Q&A
Nov 1, 2024 · 4 comments · 4 replies
Discussion options

You must be logged in to vote

Hi. I ended up with this solution. Tortoise ORM natively supports wrapping a function in a single transaction using the Atomic decorator. To ensure a request is in an independent transaction (and will roll back in case of an exception), you need to wrap it with tortoise.transactions.atomic.

from tortoise import Tortoise, fields
from tortoise.models import Model
from tortoise.transactions import atomic as req_in_transaction

class TestModel(Model):
    uuid = fields.UUIDField(primary_key=True)

class TortoiseMiddleware:
    async def process_startup(self, scope, event):
        await Tortoise.init(config=TORTOISE_ORM)
        await Tortoise.generate_schemas()

    async def process_shutdown(

Replies: 4 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@0x1618
Comment options

@CaselIT
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by 0x1618
Comment options

You must be logged in to vote
2 replies
@0x1618
Comment options

@vytas7
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #2398 on November 03, 2024 10:47.