Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Django 1.7加载初始数据和数据迁移_Python_Json_Django_Migration_Data Migration - Fatal编程技术网

Python 使用Django 1.7加载初始数据和数据迁移

Python 使用Django 1.7加载初始数据和数据迁移,python,json,django,migration,data-migration,Python,Json,Django,Migration,Data Migration,我最近从Django 1.6切换到1.7,并开始使用迁移(我从未使用过South) from django.core.management import call_command fixture = 'initial_data' def load_fixture(apps, schema_editor): call_command('loaddata', fixture, app_label='yourapp') 在1.7之前,我使用fixture/initial_data.js

我最近从Django 1.6切换到1.7,并开始使用迁移(我从未使用过South)

from django.core.management import call_command

fixture = 'initial_data'

def load_fixture(apps, schema_editor):
    call_command('loaddata', fixture, app_label='yourapp') 
在1.7之前,我使用
fixture/initial_data.json
文件加载初始数据,该文件是通过
python manage.py syncdb
命令加载的(在创建数据库时)

from django.core.management import call_command

fixture = 'initial_data'

def load_fixture(apps, schema_editor):
    call_command('loaddata', fixture, app_label='yourapp') 
现在,我开始使用迁移,不推荐使用这种行为:

from django.core.management import call_command

fixture = 'initial_data'

def load_fixture(apps, schema_editor):
    call_command('loaddata', fixture, app_label='yourapp') 
如果应用程序使用迁移,则不会自动加载装置。 由于Django 2.0中的应用程序需要迁移,因此这种行为被认为是不推荐的。如果要加载应用程序的初始数据,请考虑在数据迁移中进行。 ()

from django.core.management import call_command

fixture = 'initial_data'

def load_fixture(apps, schema_editor):
    call_command('loaddata', fixture, app_label='yourapp') 
政府没有一个明确的例子来说明如何做到这一点,因此我的问题是:

from django.core.management import call_command

fixture = 'initial_data'

def load_fixture(apps, schema_editor):
    call_command('loaddata', fixture, app_label='yourapp') 
使用数据迁移导入此类初始数据的最佳方法是什么:

from django.core.management import call_command

fixture = 'initial_data'

def load_fixture(apps, schema_editor):
    call_command('loaddata', fixture, app_label='yourapp') 
  • 编写Python代码,并多次调用mymodel.create(…)
  • 使用或编写Django函数()从JSON fixture文件加载数据
  • 我更喜欢第二种选择

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    

    我不想使用South,因为Django现在似乎可以在本地使用它。

    在我看来,固定装置有点糟糕。如果您的数据库经常更改,那么让它们保持最新将很快成为一场噩梦。事实上,这不仅仅是我的观点,在《两勺Django》一书中,它的解释要好得多

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    相反,我将编写一个Python文件来提供初始设置。如果你还需要什么,我建议你看看

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    如果需要迁移某些数据,则应使用

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    

    还有关于使用fixture的内容。

    为了给数据库提供一些初始数据,请编写一个
    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    在数据迁移中,使用函数加载数据

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    不要编写任何loaddata命令,因为不推荐使用这种方式

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    您的数据迁移将只运行一次。迁移是一个有序的迁移序列。运行003_xxxx.py迁移时,django migrations会在数据库中写入此应用已迁移到此应用(003),并将仅运行以下迁移。

    更新:有关此解决方案可能导致的问题,请参阅下面的@GwynBleidD注释,请参阅下面@Rockallite的答案,以了解一种对未来模型更改更持久的方法

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    

    假设您在
    /fixtures/initial_data.json

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
  • 创建空迁移:

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    在Django 1.7中:

    python manage.py makemigrations --empty <yourapp>
    
    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    2.2。
    负载夹具的更简单解决方案(根据@juliocesar的建议):

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    如果要使用自定义目录,此选项非常有用

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    2.3最简单:使用
    app\u标签调用
    loaddata
    将自动从
    装置的
    目录加载装置:

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    如果您未指定
    app\u标签
    ,loaddata将尝试从所有apps fixture目录加载
    fixture
    文件名(您可能不需要)

  • from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
  • 运行它

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    python manage.py migrate <yourapp>
    
    python manage.py迁移
    

  • 受一些评论(即n___o)的启发,以及我拥有大量
    初始数据。*
    文件分布在多个应用程序上的事实,我决定创建一个Django应用程序,以方便创建这些数据迁移

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    ./manage.py create_initial_data_fixtures
    Migrations for 'eggs':
      0002_auto_20150107_0817.py:
    Migrations for 'sausage':
      Ignoring 'initial_data.yaml' - migration already exists.
    Migrations for 'foo':
      Ignoring 'initial_data.yaml' - not migrated.
    
    使用,您只需运行以下管理命令,它将搜索所有
    已安装的应用程序
    ,查找
    初始数据。*
    文件并将其转换为数据迁移

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    ./manage.py create_initial_data_fixtures
    Migrations for 'eggs':
      0002_auto_20150107_0817.py:
    Migrations for 'sausage':
      Ignoring 'initial_data.yaml' - migration already exists.
    Migrations for 'foo':
      Ignoring 'initial_data.yaml' - not migrated.
    

    有关安装/使用说明,请参阅。

    不幸的是,上面介绍的解决方案对我不起作用。我发现,每次我改变我的模型,我必须更新我的装置。理想情况下,我会编写数据迁移,以类似地修改创建的数据和夹具加载的数据

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    为方便此操作,将在当前应用程序的
    fixtures
    目录中查找并加载fixture。将此函数放入与迁移中的字段匹配的模型历史点的迁移中。

    Short version
    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    您不应该在数据迁移中直接使用
    loaddata
    管理命令

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    #数据迁移的坏例子
    从django.db导入迁移
    从django.core.management导入call_命令
    def加载装置(应用程序、模式编辑器):
    #不,这是错误的。别这样!
    调用_命令('loaddata','your_data.json',app_label='yourapp')
    类迁移(migrations.Migration):
    依赖项=[
    #对其他迁移的依赖关系
    ]
    操作=[
    RunPython(load_fixture),
    ]
    
    长版本
    loaddata
    利用了
    django.core.serializers.python.Deserializer
    ,它在迁移中使用最新的模型来反序列化历史数据。那是不正确的行为

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    例如,假设有一个数据迁移,它利用
    loaddata
    management命令从夹具加载数据,并且它已经应用到您的开发环境中

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    稍后,您决定向相应的模型添加一个新的必填字段,因此您可以这样做,并根据更新的模型进行新的迁移(并且在
    /manage.py makemigrations
    提示时,可能会向新字段提供一个一次性值)

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    您运行了下一次迁移,一切正常

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    最后,完成了Django应用程序的开发,并将其部署到生产服务器上。现在是您在生产环境中从头开始运行整个迁移的时候了

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    但是,数据迁移失败。这是因为来自
    loaddata
    命令的反序列化模型(表示当前代码)不能与您添加的新必填字段的空数据一起保存。原始夹具缺少必要的数据

    from django.core.management import call_command
    
    fixture = 'initial_data'
    
    def load_fixture(apps, schema_editor):
        call_command('loaddata', fixture, app_label='yourapp') 
    
    但是,即使使用新字段所需的数据更新fixture,数据迁移仍然失败。当数据迁移运行时,下一次迁移将添加相应的