GithubHelp home page GithubHelp logo

Comments (4)

vlakius avatar vlakius commented on June 5, 2024 1

ok

  1. Create a folder structure like this:
├── app
│   ├── models.py
├── db.sqlite3
├── main.py

in models.py

from tortoise import fields
from tortoise.models import Model


class Tournament(Model):
    # Defining `id` field is optional, it will be defined automatically
    # if you haven't done it yourself
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255)

    # Defining ``__str__`` is also optional, but gives you pretty
    # represent of model in debugger and interpreter
    def __str__(self):
        return self.name


class Event(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255)
    # References to other models are defined in format
    # "{app_name}.{model_name}" - where {app_name} is defined in tortoise config
    tournament = fields.ForeignKeyField("models.Tournament", related_name="events")
    participants = fields.ManyToManyField(
        "models.Team", related_name="events", through="event_team"
    )

    def __str__(self):
        return self.name


class Team(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255)

    def __str__(self):
        return self.name

in main.py

from tortoise import Tortoise, run_async

from app.models import Event, Team, Tournament


async def init():
    # Here we create a SQLite DB using file "db.sqlite3"
    #  also specify the app name of "models"
    #  which contain models from "app.models"
    await Tortoise.init(
        db_url="sqlite://db.sqlite3", modules={"models": ["app.models"]}
    )
    # Generate the schema
    await Tortoise.generate_schemas()


async def test_task():
    # Create instance by save
    tournament = Tournament(name="New Tournament")
    await tournament.save()

    # Or by .create()
    await Event.create(name="Without participants", tournament=tournament)
    event = await Event.create(name="Test", tournament=tournament)
    participants = []
    for i in range(2):
        team = await Team.create(name="Team {}".format(i + 1))
        participants.append(team)

    # M2M Relationship management is quite straightforward
    # (look for methods .remove(...) and .clear())
    await event.participants.add(*participants)

    # You can query related entity just with async for
    async for team in event.participants:
        pass

    # After making related query you can iterate with regular for,
    # which can be extremely convenient for using with other packages,
    # for example some kind of serializers with nested support
    for team in event.participants:
        pass

    # Or you can make preemptive call to fetch related objects,
    # so you can work with related objects immediately
    selected_events = await Event.filter(
        participants=participants[0].id
    ).prefetch_related("participants", "tournament")
    for event in selected_events:
        print(event.tournament.name)
        print([t.name for t in event.participants])

    # Tortoise ORM supports variable depth of prefetching related entities
    # This will fetch all events for team and in those team tournament will be prefetched
    await Team.all().prefetch_related("events__tournament")

    # You can filter and order by related models too
    await Tournament.filter(events__name__in=["Test", "Prod"]).order_by(
        "-events__participants__name"
    ).distinct()


async def main():
    await init()
    await test_task()


if __name__ == "__main__":
    run_async(main())

I agree that the documentation examples can be improved

from tortoise-orm.

vlakius avatar vlakius commented on June 5, 2024

This is not related to tortoise orm.

from the python docs:

6.4. Await expression
Suspend the execution of coroutine on an awaitable object. Can only be used inside a coroutine function.

from tortoise-orm.

ZackPlauche avatar ZackPlauche commented on June 5, 2024

This is not related to tortoise orm.

from the python docs:

6.4. Await expression
Suspend the execution of coroutine on an awaitable object. Can only be used inside a coroutine function.

I get that the error might be from somewhere else... But I would expect the example code from the tutorial to work and be useful otherwise it's useless.

The code I wrote is directly from the docs at the time of me posting it.

How can I make it work? What did I do wrong?

from tortoise-orm.

ZackPlauche avatar ZackPlauche commented on June 5, 2024

Thanks!

from tortoise-orm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.