Opened 10 years ago

Last modified 3 years ago

#24778 new New feature

Data Migration from Fixture — at Initial Version

Reported by: Eugene Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Eugene, Alex Dehnert Triage Stage: Someday/Maybe
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no
Pull Requests:How to create a pull request

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'),
    ]

According to the ticket's flags, the next step(s) to move this issue forward are:

  • Unknown. The Someday/Maybe triage stage is used to keep track of high-level ideas or long term feature requests.

    It could be an issue that's blocked until a future version of Django (if so, Keywords will contain that version number). It could also be an enhancement request that we might consider adding someday to the framework if an excellent patch is submitted.

    If you're interested in contributing to the issue, raising your ideas on the Django Forum would be a great place to start.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top