我正在尝试为 Django 项目创建测试装置。
我有一个Question 子对象和一个Node 父对象。
由于某些自定义管理器,无法使用manage.py dumpdata捕获父对象 Node 。
我正在尝试创建自己的夹具转储管理命令来捕获父对象和子对象。
如何将更多对象附加到 json 夹具文件?
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()
这是节点管理器代码:
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?