I am trying to create test fixtures for a Django project.
I have a Question child object and a Node parent object.
Because of some custom managers, the parent object Node are not captured with manage.py dumpdata.
I'm attempting to create my own fixture dump management command to capture the parent objects as well as the children.
How do I append more objects to a json fixture file?
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()
Here is the NodeManager code:
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)
Why are the parent object Nodes not being writen to the serialized_list?