how to import serveral csv into database by using migrations.RunPython

11 views
Skip to first unread message

emmanuel wyckens

unread,
Aug 22, 2018, 11:32:05 AM8/22/18
to Django users
Hello,

The class below didn't work if you use  migrations.RunPython several times.
Have you got an idea to manage double csv file importation into database ?

Thanks

Emmanuel

class Migration(migrations.Migration):

    dependencies = [
        ("contact", "0001_initial"),
    ]

    operations = [
         migrations.RunPython(
               lambda apps, schema_editor: import_countries(
               str(BASE_DIR / "contact" / "migrations" / "departement.csv")
             )
         ),
         migrations.RunPython(
             lambda apps, schema_editor: import_zipcode(
                 str(BASE_DIR / "contact" / "migrations" / "codepostal.csv")
             )
         ),
    ]

Mike Dewhirst

unread,
Aug 22, 2018, 6:50:35 PM8/22/18
to django...@googlegroups.com
On 23/08/2018 1:32 AM, emmanuel wyckens wrote:
> Hello,
>
> The class below didn't work if you use migrations.RunPython several times.
> Have you got an idea to manage double csv file importation into database ?

In your migration file ...

from django.db import migrations, models
from <app>.utils import double_csv_file_importation

class Migration(migrations.Migration):
    dependencies = [
        ('<app>', '<previous_migration>'),
    ]
    operations = [
        migrations.RunPython(
            double_csv_file_importation,
            # afterwards, edit the first line of
double_csv_file_importation to return None
            # so that subsequent migrations don't re-import the csv files
        ),
    ]

I use this way frequently to import changing reference data from
multiple csv files. It lets me build and test the importation process
separately and just trigger it from a new migration whenever necessary.

You may also consider using a management command. I also uses these for
csv imports. It is slightly more complex to set up - but well documented
- but easier to manage if you have access to the server to run them
whenever necessary.

>
> Thanks
>
> Emmanuel
>
> class Migration(migrations.Migration):
>
>     dependencies = [
>         ("contact", "0001_initial"),
>     ]
>
>     operations = [
>          migrations.RunPython(
>                lambda apps, schema_editor: import_countries(
>                str(BASE_DIR / "contact" / "migrations" /
> "departement.csv")
>              )
>          ),
>          migrations.RunPython(
>              lambda apps, schema_editor: import_zipcode(
>                  str(BASE_DIR / "contact" / "migrations" /
> "codepostal.csv")
>              )
>          ),
>     ]
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a0c3fd68-9db2-462d-b122-9ea439e61ce4%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/a0c3fd68-9db2-462d-b122-9ea439e61ce4%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages