Thursday, March 5, 2015

Re: Data migration using RunPython

Hello,

In Django, you can instanciate objects from your model without
persist it to the database. The way you do it is Country(name=..., ...).

In order to create multiple objects at once, you can
call the bulk_create method on the manager with a list
of object to persist.

The using(db_alias) is instanciating the manager.
I won't explain further cause my knowledge of this area
is reduced.

Have a good one

On 03/05/2015 01:20 AM, Murthy Sandeep wrote:
> I am working on a data migration for my Django app to populate
> the main table in the db with data that will form the mainstay of
> the app - this is persistent/permanent data that may added to but
> never deleted.
>
> My reference is the Django 1.7 documentation and in particular an
> example on page
>
> https://docs.djangoproject.com/en/1.7/ref/migration-operations/#django.db.migrations.operations.RunPython
>
> with a custom method called forward_funcs:
>
> def forwards_func(apps, schema_editor):
>
> # We get the model from the versioned app registry;
> # if we directly import it, it'll be the wrong version
>
>
> Country = apps.get_model("myapp", "Country")
> db_alias = schema_editor.connection.alias
> Country.objects.using(db_alias).bulk_create([
> Country(name="USA", code="us"),
> Country(name="France", code="fr"),])
>
> I was wondering if someone could please explain what is happening
> here and how this method works - is the argument to bulk_create a
> list of namedtuple objects called Country or are these Country model
> objects?
>
> Also could someone please explain what db_alias is?
>
> Sandeep
>
>

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/54F814FC.9090407%40arkade.info.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment