Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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 使用fixture或脚本预填充数据库?_Python_Django_Database_Django Testing_Django Migrations - Fatal编程技术网

Python 使用fixture或脚本预填充数据库?

Python 使用fixture或脚本预填充数据库?,python,django,database,django-testing,django-migrations,Python,Django,Database,Django Testing,Django Migrations,我不是专家,但我认为使用类定义选项并用这些选项预填充数据库是个好主意。我认为这样更容易改变选择,等等 因此,在我的models.py中,我有: class City(models.Model): name = models.CharField(max_length=32) distance = models.SmallIntegerField(blank=True, null=True) #etc class OtherClass(models.Model):

我不是专家,但我认为使用类定义选项并用这些选项预填充数据库是个好主意。我认为这样更容易改变选择,等等

因此,在我的
models.py中,我有:

class City(models.Model):
    name = models.CharField(max_length=32)
    distance = models.SmallIntegerField(blank=True, null=True)
    #etc

class OtherClass(models.Model):
    name = models.CharField(max_length=32)
    #etc

class UserProfile(models.Model):
    name = models.CharField(max_length=32)
    city = models.ForeignKey(City)
    otherfield = models.ForeignKey(OtherClass)
    #etc
UserProfile
是用户编译的,
City
OtherClass
是程序员放置选项的地方

迁移之后,我必须创建一些
City
OtherClass
对象:它们将是选项(是的,它们必须被修复)

我只是想知道这件事。到目前为止,我一直在使用
脚本

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sitopossedimenti.settings')

import django
django.setup()

from core.models import *

def populate():
    namecity1 = add_source('city1', None)
    namecity2 = add_source('city2', None)
    @etc

    nameotherclass1 = add_otherclass('name1', #etc)

    #etc some thousands more

def add_source(name, distance):
    s = model.Source.objects.get_or_create(name=name, distance=distance)[0]
    s.save()
    return s

def add_otherclass:
    #etc

if __name__ == '__main__':
    print ("Starting myapp population script...")
    populate()

现在剧本可以用了,我害怕改变。。。但是你觉得呢?固定装置更好吗?为什么?有区别吗?

俗话说,如果它有效,就不要修复它。固定装置是更常用的方法,但使用自己的固定装置并无害处。如果您正在编写一个新的测试用例,您可能希望使用fixture,但如果我是您,我会让它成为现实

<>如果你想要一个完全自动化的方法来达到这个结果,考虑一下。链接文档包含一个完整示例,显示正在加载的数据。显然,
/manage.py migrate
会发生这种情况,而不需要额外的步骤

使用migrations.RunPython的优点是,如果您要与同事共享应用程序或在其他服务器上安装,则所需的数据将自动加载到生产服务器中,并且测试还可以在测试数据库中对其进行完全访问