Reputation: 193
How can i create a xml file in django where the file is to contain objects from a queryset?
def my_serialize(request):
from django.core import serializers
data = serializers.serialize('xml', Student.objects.filter(Q(name__startswith='A')),
fields=('name','dob'))
from django.core.files import File
f = open('content.xml', 'w')
myfile = File(f)
myfile.write(data)
myfile.close()
After i call the above function, my content file remains empty, there is no data that gets written in it.
Upvotes: 4
Views: 6032
Reputation: 31951
from django.core import serializers
data = serializers.serialize("xml", SomeModel.objects.all())
Take a look at the Serialization documentation.
Upvotes: 2
Reputation: 4226
I don't use version 1.3, so I'm not sure how it works, but could it be that the file is not actually being opened? Could adding myfile.open('w')
work? Of course, this may be handled in the File class's init function. Anyway, try giving it a shot and commenting your results.
Update: BTW, I came up with the idea from looking at the docs. Maybe they'll be able to help too. http://docs.djangoproject.com/en/1.3/ref/files/file/
Upvotes: 0