Skip to content
Adrian Wilke edited this page Sep 12, 2024 · 1 revision

Data

Serialize to fixtures

python manage.py shell
from django.core import serializers
from events.models import Country
data = serializers.serialize("xml", Country.objects.all())

f = open("events/fixtures/country.xml", "w") 
f.write(data)
f.close()

exit()
print(data)
# <?xml version="1.0" encoding="utf-8"?>
# <django-objects version="1.0"><object model="events.country" pk="1"><field name="title" type="CharField">Germany</field></object></django-objects>

Deserialize from fixtures

python manage.py shell
from django.core import serializers

f = open("events/fixtures/country.xml", "r")
data = f.read()
f.close()

for obj in serializers.deserialize("xml", data):
  obj.save()

exit()
Clone this wiki locally