django中的initial_data fixture管理

时间:2022-09-20 13:49:48

The django projet I'm working on has a ton of initial_data fixture data. It seems by default the only way to have data load automatically is to have a file in your app folder called fixtures, and the file needs to be named initial_data.ext (ext being xml or json or yaml or something).

我正在研究的django projet有大量的initial_data夹具数据。默认情况下,自动加载数据的唯一方法是在app文件夹中有一个名为fixture的文件,文件需要命名为initial_data.ext(ext为xml或json或yaml等)。

This is really unflexable, I think. I'd rather have a fixtures folder, and then inside that a initial_data folder, and then inside there, one file for each model in that app. Or something to that effect. IS this possible to do in django now? Or maybe some other scheme of better fixture organization.

我认为这真的很难实现。我宁愿有一个fixtures文件夹,然后在一个initial_data文件夹里面,然后在那里,为该应用程序中的每个模型一个文件。或者那种效果。现在这可以在django做吗?或者可能是其他一些更好的夹具组织方案。

4 个解决方案

#1


9  

In my experience, hard-coded fixtures are a pain to write and a pain to maintain. Wherever a model change breaks a fixture, the Django initial load will return a very unfriendly error message and you will end-up adding a bunch a of print's in the Django core in order to find where the problem is coming from.

根据我的经验,硬编码的灯具很难写,也很难维护。无论何时模型更改破坏了夹具,Django初始加载都将返回非常不友好的错误消息,并且最终会在Django核心中添加一堆打印件以便找到问题的来源。

One of the developer I work with has developed a very good library to overcome this problem, it's called django-dynamic-fixture and we really to love it. Here is how it works:

我与之合作的开发人员之一开发了一个非常好的库来克服这个问题,它被称为django-dynamic-fixture,我们真的很喜欢它。下面是它的工作原理:

Suppose you have this models:

假设你有这个模型:

class Author(models.Model):
    name = models.CharField()

class Book(models.Model):
    author = models.ForeingKey(Author, required=True)
    title = models.CharField()

In order to create a book instance in your test, all you have to do is

要在测试中创建书籍实例,您所要做的就是

from django_dynamic_fixture import get
from app import Book

class MyTest(TestCase):
    def setUp(self):
        self.book = get(Book)

The django-dynamic-fixture automatically creates for you any dependencies that are required for the Book model to exist. This is just a simple example but the library can handle very complex model structures.

django-dynamic-fixture会自动为您创建Book模型所需的任何依赖项。这只是一个简单的例子,但库可以处理非常复杂的模型结构。

#2


2  

You can reorganize your initial data fixtures however you want and then write a post_syncdb signal handler which loads them. So it will then be automatically loaded on syncdb, according to the logic defined by you.

您可以根据需要重新组织初始数据夹具,然后编写一个post_syncdb信号处理程序来加载它们。因此,根据您定义的逻辑,它将自动加载到syncdb上。

See: https://docs.djangoproject.com/en/1.3/ref/signals/#post-syncdb

#3


0  

Yes, you can split fixtures into multiple files with subfolder structures. You can specify fixture files to load and create a script that loads some or all of them. I have done this before so can confirm that it works.

是的,您可以使用子文件夹结构将灯具拆分为多个文件。您可以指定要加载的夹具文件,并创建一个加载其中一些或全部的脚本。我之前已经这样做了,所以可以确认它有效。

Example: django-admin.py loaddata application/module/model.json

示例:django-admin.py loaddata application / module / model.json

See loaddata documentation for more information.

有关更多信息,请参阅loaddata文档。

#4


0  

A hacky way to load an additional initial_data.json or two is to create additional empty apps within your Django project that has nothing but the fixtures folder and the initial_data.json file. If you need the fixture loaded before the other apps' fixtures, you could name it something like aa1. If you need another one you could name it aa2. Your directory structure would look like this:

加载一个或多个initial_data.json的hacky方法是在Django项目中创建除了fixtures文件夹和initial_data.json文件之外的其他空应用程序。如果您需要在其他应用程序的灯具之前加载灯具,您可以将其命名为aa1。如果你需要另一个你可以命名为aa2。您的目录结构如下所示:

aa1/
   fixtures/
      initial_data.json

aa2/
   fixtures/
      initial_data.json

myrealapp/
   fixtures/
      initial_data.json
...

You would need to add the apps to INSTALLED_APPS in settings.py.

您需要将应用程序添加到settings.py中的INSTALLED_APPS。

You can then populate the fixture_data.json files with arbitrary app information as necessary:

然后,您可以根据需要使用任意应用程序信息填充fixture_data.json文件:

(virtualenv) ./manage.py dumpdata --indent=4 auth > aa1/fixtures/initial_data.json

(virtualenv) ./manage.py dumpdata --indent=4 oauth2 > aa2/fixtures/initial_data.json

(virtualenv) ./manage.py dumpdata --indent=4 myrealapp > myrealapp/fixtures/initial_data.json

When you run python manage.py syncdb, each of the fixtures will be loaded in automatically in alphabetical order.

当您运行python manage.py syncdb时,每个灯具将按字母顺序自动加载。

As I mentioned, this is quite hacky, but if you only need a couple extra initial_data.json files and need to be able to control the order they are loaded in, this works.

正如我所提到的,这是非常hacky,但如果你只需要一些额外的initial_data.json文件并且需要能够控制它们被加载的顺序,这就行了。

#1


9  

In my experience, hard-coded fixtures are a pain to write and a pain to maintain. Wherever a model change breaks a fixture, the Django initial load will return a very unfriendly error message and you will end-up adding a bunch a of print's in the Django core in order to find where the problem is coming from.

根据我的经验,硬编码的灯具很难写,也很难维护。无论何时模型更改破坏了夹具,Django初始加载都将返回非常不友好的错误消息,并且最终会在Django核心中添加一堆打印件以便找到问题的来源。

One of the developer I work with has developed a very good library to overcome this problem, it's called django-dynamic-fixture and we really to love it. Here is how it works:

我与之合作的开发人员之一开发了一个非常好的库来克服这个问题,它被称为django-dynamic-fixture,我们真的很喜欢它。下面是它的工作原理:

Suppose you have this models:

假设你有这个模型:

class Author(models.Model):
    name = models.CharField()

class Book(models.Model):
    author = models.ForeingKey(Author, required=True)
    title = models.CharField()

In order to create a book instance in your test, all you have to do is

要在测试中创建书籍实例,您所要做的就是

from django_dynamic_fixture import get
from app import Book

class MyTest(TestCase):
    def setUp(self):
        self.book = get(Book)

The django-dynamic-fixture automatically creates for you any dependencies that are required for the Book model to exist. This is just a simple example but the library can handle very complex model structures.

django-dynamic-fixture会自动为您创建Book模型所需的任何依赖项。这只是一个简单的例子,但库可以处理非常复杂的模型结构。

#2


2  

You can reorganize your initial data fixtures however you want and then write a post_syncdb signal handler which loads them. So it will then be automatically loaded on syncdb, according to the logic defined by you.

您可以根据需要重新组织初始数据夹具,然后编写一个post_syncdb信号处理程序来加载它们。因此,根据您定义的逻辑,它将自动加载到syncdb上。

See: https://docs.djangoproject.com/en/1.3/ref/signals/#post-syncdb

#3


0  

Yes, you can split fixtures into multiple files with subfolder structures. You can specify fixture files to load and create a script that loads some or all of them. I have done this before so can confirm that it works.

是的,您可以使用子文件夹结构将灯具拆分为多个文件。您可以指定要加载的夹具文件,并创建一个加载其中一些或全部的脚本。我之前已经这样做了,所以可以确认它有效。

Example: django-admin.py loaddata application/module/model.json

示例:django-admin.py loaddata application / module / model.json

See loaddata documentation for more information.

有关更多信息,请参阅loaddata文档。

#4


0  

A hacky way to load an additional initial_data.json or two is to create additional empty apps within your Django project that has nothing but the fixtures folder and the initial_data.json file. If you need the fixture loaded before the other apps' fixtures, you could name it something like aa1. If you need another one you could name it aa2. Your directory structure would look like this:

加载一个或多个initial_data.json的hacky方法是在Django项目中创建除了fixtures文件夹和initial_data.json文件之外的其他空应用程序。如果您需要在其他应用程序的灯具之前加载灯具,您可以将其命名为aa1。如果你需要另一个你可以命名为aa2。您的目录结构如下所示:

aa1/
   fixtures/
      initial_data.json

aa2/
   fixtures/
      initial_data.json

myrealapp/
   fixtures/
      initial_data.json
...

You would need to add the apps to INSTALLED_APPS in settings.py.

您需要将应用程序添加到settings.py中的INSTALLED_APPS。

You can then populate the fixture_data.json files with arbitrary app information as necessary:

然后,您可以根据需要使用任意应用程序信息填充fixture_data.json文件:

(virtualenv) ./manage.py dumpdata --indent=4 auth > aa1/fixtures/initial_data.json

(virtualenv) ./manage.py dumpdata --indent=4 oauth2 > aa2/fixtures/initial_data.json

(virtualenv) ./manage.py dumpdata --indent=4 myrealapp > myrealapp/fixtures/initial_data.json

When you run python manage.py syncdb, each of the fixtures will be loaded in automatically in alphabetical order.

当您运行python manage.py syncdb时,每个灯具将按字母顺序自动加载。

As I mentioned, this is quite hacky, but if you only need a couple extra initial_data.json files and need to be able to control the order they are loaded in, this works.

正如我所提到的,这是非常hacky,但如果你只需要一些额外的initial_data.json文件并且需要能够控制它们被加载的顺序,这就行了。