Thursday, March 5, 2015

Re: Data migration using RunPython

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/

>> On 5 Mar 2015, at 19:34, aRkadeFR <contact@arkade.info> wrote:
>>
>> 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.

--
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/54F824F6.7040109%40arkade.info.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment