Wednesday, March 4, 2015

Data migration using RunPython

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.26
Comment: GPGTools - https://gpgtools.org

iQIcBAEBCgAGBQJU96FEAAoJEE/+9O67CIlEi4IQAJWNC9ur777/EmH4C/ESaW0q
b4lLD0J5osaFHYJoMIbk3oDuAlq1gOrjQDAl6wTZEMRTebeaTcDdDdkFuWYNrnAJ
nBy6BUJjXj4OPi2xe3QYVlt4J7VrRxS4zL2O97nAdnp36jczJNtJ+kP1eQ7iiod7
SAXvabUuVN8OmjOEBJl+55qVLMpdxeXUS/g3IgbT5rEH2jvT0iQKtCqQyNSNyM02
FUj0R4AXflqipYTq078KPhDOhSMl4OxltZM0+O2DwNw57u0kqN1td7kBDRYeF6N+
xROay4a/n5swgupG5P1TxiY38z3OdqUqqk7cn5U7W2utFFRBD2Di9uWG5qcBLbqY
G/lrmzhjq6H0aoPS7tPjn6E9Y9HGBOYjF53YA1f16MGMqb031SHrByKjb6ZTcRGu
FMYrt+L8yZ5RTh+U0NhgXBjTRHlTi33ls9kIiUwMnx9RC379ccAX9p4zuTJce/gk
JDatcbwOUtfmti3a7sfDwu8j7xIx+kVNYB2xbagRWaJYWV83Y2yXV95z/NeTf68S
tySUoAoOtmAsjeXdU/QjxgtsS21pIWKo2/Ih+Xpnq3+u0FMDYU3Y6fvrC1fUywvS
LAkrZMOvCWo4A8YdOZA2pdD525DybIwgNscVHfzEiwC8sJO/ziXmyvY0ah4duBsf
JyN1zrqVNvw2/zbL9aO1
=ec9r
-----END PGP SIGNATURE-----
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/79F2F719-8483-46A6-8DC2-94E6C937CAA8%40sandeepmurthy.is.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment