Data migration using RunPython

165 views
Skip to first unread message

Murthy Sandeep

unread,
Mar 4, 2015, 7:23:33 PM3/4/15
to django...@googlegroups.com
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


signature.asc

aRkadeFR

unread,
Mar 5, 2015, 3:37:18 AM3/5/15
to django...@googlegroups.com
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

Murthy Sandeep

unread,
Mar 5, 2015, 4:03:35 AM3/5/15
to django...@googlegroups.com
Thanks,

Two further questions if you don’t mind:

1. I am in the Python interpreter and am working with
one of my db models called SmallGroup.

When I do SmallGroup.objects.all() I get the following message:

File "<console>", line 1, in <module>
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 119, in __repr__
return repr(data)
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 458, in __repr__
u = six.text_type(self)
TypeError: coercing to Unicode: need string or buffer, SmallGroup found

Does this have something to do with the method `__str__` which I added
to this model in my `models.py`? I don’t think I did a ‘python manage.py migrate’ after
the change though.

2. If I want to modify or delete entries in the db table SmallGroup are there
methods corresponding to `bulk_create` for doing this? Something like

Country.objects.using(db_alias).bulk_update( <list> )

Country.objects.using(db_alias).bulk_delete( <list> )

?

Thanks again in advance.

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...@googlegroups.com.
> To post to this group, send email to django...@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.

signature.asc

aRkadeFR

unread,
Mar 5, 2015, 4:43:38 AM3/5/15
to django...@googlegroups.com

On 03/05/2015 10:01 AM, Murthy Sandeep wrote:
> Thanks,
>
> Two further questions if you don’t mind:
>
> 1. I am in the Python interpreter and am working with
> one of my db models called SmallGroup.
>
> When I do SmallGroup.objects.all() I get the following message:
>
> File "<console>", line 1, in <module>
> File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 119, in __repr__
> return repr(data)
> File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 458, in __repr__
> u = six.text_type(self)
> TypeError: coercing to Unicode: need string or buffer, SmallGroup found
>
> Does this have something to do with the method `__str__` which I added
> to this model in my `models.py`? I don’t think I did a ‘python manage.py migrate’ after
> the change though.

There's two problems yeah, you didn't run your migrations, and maybe
you have unicode encoding problem.
Please, check the documentation on migration[1] and unicode[2].

>
> 2. If I want to modify or delete entries in the db table SmallGroup are there
> methods corresponding to `bulk_create` for doing this? Something like
>
> Country.objects.using(db_alias).bulk_update( <list> )
>
> Country.objects.using(db_alias).bulk_delete( <list> )
>
> ?

You have all you need in the documentation [3].
You have the .update and .delete methods on a queryset.

> Thanks again in advance.
>
> Sandeep

[1] migrations: https://docs.djangoproject.com/en/1.7/topics/migrations/
[2] unicode with python 2: https://docs.python.org/2/howto/unicode.html
[3] https://docs.djangoproject.com/en/1.7/topics/db/queries/
Reply all
Reply to author
Forward
0 new messages