5

我们的其中一台服务器有 4 GB 的数据。但到目前为止,我只对填充固定装置的少量数据感兴趣。一种简单的转储数据是:

 python manage.py dumpdata --indent=4 > shipping_fixture.json

但这样做的问题是它将所有数据转储到数据库中。在测试中处理如此大量的数据没有任何意义。有什么办法可以限制我不会让事情变得沉重的数据量,而且我下载的数据本身就是完整的。

4

2 回答 2

4

The latest django (1.5.5) doesn't have such an option, but I think it's coming up soon in a future version. There is currently a ticket implementing a new feature to the dumpdata command that will allow you to filter what gets outputted based on the primary key if the model is specified.

A 3rd party app called django-test-utils can probably do what you need.

Use Django dumpdata to dump a subset of overall data?

于 2012-06-21T14:39:05.833 回答
1

我建议使用 django 序列化 [1]。它有助于将自定义查询转储到数据文件中。

要转储数据:

from django.core import serializers

with open("/tmp/file.json", "w") as f:
    serializers.serialize('json', query, stream=f)

并加载数据:

with open("file.json", "r") as file:
    data = file.read()

for obj in serializers.deserialize("json", data):
    do_something_with_the_object(obj)

[1] https://docs.djangoproject.com/en/dev/topics/serialization/

于 2015-09-09T17:13:12.883 回答