Examples of serialized objects
Examples of data serialized with Django's serialization framework.
These all use the Poll
example from tutorial 1:
fromdjango.dbimport models classPoll(models.Model): question = models.CharField(maxlength=200) pub_date = models.DateTimeField('date published') classChoice(models.Model): poll = models.ForeignKey(Poll) choice = models.CharField(maxlength=200) votes = models.IntegerField()
XML
Though it's the most verbose, the XML format is considered somewhat canonical; all the other formats leave out some of the aspects of the XML format:
<?xml version="1.0" encoding="utf-8"?> <django-objectsversion="1.0"> <objectpk="1"model="myapp.poll"> <fieldtype="CharField"name="question">Whatisyourfavoritecolor?</field> <fieldtype="DateTimeField"name="pub_date">2007年06月06日11:23:42</field> </object> <objectpk="2"model="myapp.poll"> <fieldtype="CharField"name="question">What'syourfavoritewebframework?</field> <fieldtype="DateTimeField"name="pub_date">2007年06月06日11:24:17</field> </object> <objectpk="1"model="myapp.choice"> <fieldto="myapp.poll"name="poll"rel="ManyToOneRel">1</field> <fieldtype="CharField"name="choice">Red</field> <fieldtype="IntegerField"name="votes">5</field> </object> <objectpk="2"model="myapp.choice"> <fieldto="myapp.poll"name="poll"rel="ManyToOneRel">1</field> <fieldtype="CharField"name="choice">Blue</field> <fieldtype="IntegerField"name="votes">1</field> </object> <objectpk="3"model="myapp.choice"> <fieldto="myapp.poll"name="poll"rel="ManyToOneRel">1</field> <fieldtype="CharField"name="choice">Pink...no,yellow...</field> <fieldtype="IntegerField"name="votes">3</field> </object> <objectpk="4"model="myapp.choice"> <fieldto="myapp.poll"name="poll"rel="ManyToOneRel">2</field> <fieldtype="CharField"name="choice">Django</field> <fieldtype="IntegerField"name="votes">1000000</field> </object> <objectpk="5"model="myapp.choice"> <fieldto="myapp.poll"name="poll"rel="ManyToOneRel">2</field> <fieldtype="CharField"name="choice">Rails</field> <fieldtype="IntegerField"name="votes">0</field> </object> </django-objects>
JSON
Though XML is canonical, JSON is probably more useful for everyday use. There's not a lot of metadata, but if you know what the objects are, it works great.
[ { "pk":"1", "model":"se.poll", "fields":{ "pub_date":"2007年06月06日 11:23:42", "question":"What is your favorite color?" } }, { "pk":"2", "model":"se.poll", "fields":{ "pub_date":"2007年06月06日 11:24:17", "question":"What's your favorite web framework?" } }, { "pk":"1", "model":"se.choice", "fields":{ "votes":5, "poll":1, "choice":"Red" } }, { "pk":"2", "model":"se.choice", "fields":{ "votes":1, "poll":1, "choice":"Blue" } }, { "pk":"3", "model":"se.choice", "fields":{ "votes":3, "poll":1, "choice":"Pink... no, yellow..." } }, { "pk":"4", "model":"se.choice", "fields":{ "votes":1000000, "poll":2, "choice":"Django" } }, { "pk":"5", "model":"se.choice", "fields":{ "votes":0, "poll":2, "choice":"Rails" } } ]
YAML
Only available if PyYAML is installed, this holds mostly the same data as the JSON serializer. However, YAML's probably easier to write by hand, so it's the most useful for fixtures.
- model: se.poll pk: '1' fields: pub_date: !!timestamp '2007-06-06 11:23:42' question: 'What is your favorite color?' - model: se.poll pk: '2' fields: pub_date: !!timestamp '2007-06-06 11:24:17' question: 'What''s your favorite web framework?' - model: se.choice pk: '1' fields: choice: Red poll: 1 votes: 5 - model: se.choice pk: '2' fields: choice: Blue poll: 1 votes: 1 - model: se.choice pk: '3' fields: choice: 'Pink... no, yellow...' poll: 1 votes: 3 - model: se.choice pk: '4' fields: choice: Django poll: 2 votes: 1000000 - model: se.choice pk: '5' fields: choice: Rails poll: 2 votes: 0