Thursday, March 5, 2015

Re: Data migration using RunPython

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

iQIcBAEBCgAGBQJU+BtTAAoJEE/+9O67CIlESwgQAKGSoCEjNeAnAchBx6XGefBp
oBimOpFH4YLNKKQNKmRxcbC3V1B0ePHvv9KGglhqc2APX7EB86Z8QzmAxrZoshAh
ooPZ2WRyoVr93mverjqY+8nkhHKAjbAKU2xrE63LORmRnhQMandpxiJcSN//e6Oo
kd6gk0RvtaVcn/IQCMrJ0pKr6sd/pwAMyxa7eAqwOQyURIGXIvnrYhqURuaCLLvV
xQHqLnU/ZMd0M4BadmZHskQSQISxxnLTZe/QEevTBfjy5ssm/1yME+/CWISfrr4P
Wh6y799xdo7cSelF2P3tK7lnlunWuONJ6nNV4B+R8nuxRZwDnaANPlcsTviGKwFd
iYVH6sdYCBwKKWUK4FLropD/VxZnNdkb4mFaUJ7t2rYpvMy5BXp2ykw5OcJT2/EY
V2FedFtKLHBSgD3JXfBSZRbBBsJeFE6jq2STLayPVLokRzE1BrkV1SlSC2nvo0EC
l0Zf645pZEWsd9Yci/zhhrSeQrqhk7ge9z08Ns0fZZP+zF5OeYU+eJWBrczr2yeZ
4AXkmZQ3A9kIkA+dx1anVqUtHOE0s80SkNoRpuuyLCpcPwai6REw+fOxR3Z+XOHQ
DfMRsmy3rHMfmEIixChQ2E2rLLoFPNu6I7Knlm87ukhFXcObvztNJ3dDppN4f+9h
8qQ2aw3/+AJhrEZv9IG0
=GaeS
-----END PGP SIGNATURE-----
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

> 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/FB970506-5557-4EC1-90C2-A5918C36FC3F%40sandeepmurthy.is.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment