Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating xml file in django

Tags:

python

django

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.

like image 870
prateek Avatar asked Dec 06 '25 04:12

prateek


1 Answers

from django.core import serializers
data = serializers.serialize("xml", SomeModel.objects.all())

Take a look at the Serialization documentation.

like image 120
DrTyrsa Avatar answered Dec 08 '25 16:12

DrTyrsa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!