DEV Community

Discussion on: [Video] Asynchronous tasks in Django with Django Q (Part 3)

Collapse
 
jramiresbrito profile image
João Victor Ramires Guimarães Brito • Edited

Hey, first of all thanks for sharing!

I'm trying to populate my database using a Data Migration.

In my populate function I have 2 api calls, so the shape is something like this:

async def populate_table(apps, schema_editor):
    some stuff...
    an api call like: await get_info()...
    an api call inside a for loop (any problem with this?)
    for i in range():
       an api call like: await get_another_info()...
    more stuff...
    model.save()

And then, I have my migration class:

class Migration(migrations.Migration):

    dependencies = [
        ('app', 'the previous migration'),
    ]

    operations = [
        migrations.RunPython(populate_table)
    ]

Running the migration a Runtime Warning is raised:

RuntimeWarning: coroutine 'populate_table' was never awaited

So I try to decorate my Migration with Async Await without success.

How should I do that? And again, any problem to have async tasks inside a sync for loop?