#24778: Data Migration from Fixture
-----------------------------+--------------------------------------
Reporter: 9gix | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by 9gix):
* cc: 9gix (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> Providing data via fixtures has been deprecated. In the past, we used to
> execute the loaddata manually. After Django introduce migration, the
> recommended way to import data is to create an empty migration and use
> `RunPython` migration operations to load the data.
>
> This is a very common use case for data migration via fixture. We create
> the function just to call_command `loaddata`.
> http://stackoverflow.com/a/25981899/764592
>
> In my opinion, instead of having to create the function, we can actually
> simplify this into a migration operation on its own.
>
> As follow:
>
> {{{
> # Module: django.db.migrations.operations.base.special
> from django.core.management import call_command
> class LoadFixture(Operation):
> reduces_to_sql = False
> reversible = False
>
> def __init__(self, *fixtures):
> self.fixtures = fixtures
>
> def state_forwards(self, app_label, state):
> pass
>
> def database_forwards(self, app_label, schema_editor, from_state,
> to_state):
> for fixture in self.fixtures:
> call_command('loaddata', fixture, app_label=app_label)
>
> def database_backwards(self, app_label, schema_editor, from_state,
> to_state):
> pass
>
> def describe(self):
> return "Load Fixture Operation"
> }}}
>
> The implication of `LoadFixture` operations can be shown in the following
> example:
>
> Assuming we have the fixture in `foobar/fixtures/book_data.json`
> {{{
> # File: foobar/migrations/0002_auto_load_book.py
> class Migration(migrations.Migration):
> dependencies = [
> ('foobar', '0001_initial'),
> ]
> operations = [
> migrations.LoadFixture('book_data'),
> ]
> }}}
New description:
Providing data via fixtures has been deprecated. In the past, we used to
execute the loaddata manually. After Django introduce migration, the
recommended way to import data is to create an empty migration and use
`RunPython` migration operations to load the data.
This is a very common use case for data migration via fixture. We create
the function just to call_command `loaddata`.
http://stackoverflow.com/a/25981899/764592
In my opinion, instead of having to create the function, we can actually
simplify this into a migration operation on its own.
As follow:
{{{
# Module: django.db.migrations.operations.base.special
from django.core.management import call_command
class LoadFixture(Operation):
reduces_to_sql = False
reversible = False
def __init__(self, *fixtures):
self.fixtures = fixtures
def state_forwards(self, app_label, state):
pass
def database_forwards(self, app_label, schema_editor, from_state,
to_state):
for fixture in self.fixtures:
call_command('loaddata', fixture, app_label=app_label)
def database_backwards(self, app_label, schema_editor, from_state,
to_state):
pass
def describe(self):
return "Load Fixture Operation"
}}}
The implication of `LoadFixture` operations can be shown in the following
example:
Assuming we have the fixture in `foobar/fixtures/book_data.json`
{{{
# File: foobar/migrations/0002_auto_load_book.py
class Migration(migrations.Migration):
dependencies = [
('foobar', '0001_initial'),
]
operations = [
migrations.LoadFixture('book_data'),
]
}}}
The migration script is now much simpler.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24778#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/062.b9f152e1237bd722ae5b432aceec5930%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.