如何将更多对象附加到json文件?

时间:2012-05-15 16:08:49

标签: django json django-managers

我正在尝试为Django项目创建测试装置。

我有问题子对象节点父对象

由于某些自定义管理器,未使用 manage.py dumpdata 捕获父对象节点。

我正在尝试创建自己的夹具转储管理命令来捕获父对象以及子对象。

如何将更多对象附加到json fixture文件?

import json
from django.core import serializers
try:
    original_dump = open(dump_file.name)

    data_list = []
    for object in serializers.deserialize('json', original_dump):
        data_list.append(object)

except IOError:
    print "I can't open your file"

from forum.models import Node
for node in Node.objects.all():
    data_list.append(node)

serialized_list = serializers.serialize('json', data_list)

write_file = open(dump_file.name, "w")
write_file.write(serialized_list)
write_file.close()

这是NodeManager代码:

class NodeManager(CachedManager):
    use_for_related_fields = True

    def get_query_set(self):
        qs = NodeQuerySet(self.model)

        # If the node is an answer, question or comment we filter the Node model by type
        if self.model is not Node:
            qs = qs.filter(node_type=self.model.get_type())

        return qs

    def get_for_types(self, types, *args, **kwargs):
        kwargs['node_type__in'] = [t.get_type() for t in types]
        return self.get(*args, **kwargs)

    def filter_state(self, **kwargs):
        return self.all().filter_state(**kwargs)

为什么父对象节点没有被写入serialized_list?

0 个答案:

没有答案