Code


Version 2 (modified by jacob, 7 years ago) (diff)

Syntax highlight

Examples of serialized objects

Examples of data serialized with Django's serialization framework.

These all use the Poll example from tutorial 1:

    from django.db import models

    class Poll(models.Model):
        question = models.CharField(maxlength=200)
        pub_date = models.DateTimeField('date published')

    class Choice(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-objects version="1.0">
  <object pk="1" model="myapp.poll">
    <field type="CharField" name="question">What is your favorite color?</field>
    <field type="DateTimeField" name="pub_date">2007-06-06 11:23:42</field>
  </object>
  <object pk="2" model="myapp.poll">
    <field type="CharField" name="question">What's your favorite web framework?</field>
    <field type="DateTimeField" name="pub_date">2007-06-06 11:24:17</field>
  </object>
  <object pk="1" model="myapp.choice">
    <field to="myapp.poll" name="poll" rel="ManyToOneRel">1</field>
    <field type="CharField" name="choice">Red</field>
    <field type="IntegerField" name="votes">5</field>
  </object>
  <object pk="2" model="myapp.choice">
    <field to="myapp.poll" name="poll" rel="ManyToOneRel">1</field>
    <field type="CharField" name="choice">Blue</field>
    <field type="IntegerField" name="votes">1</field>
  </object>
  <object pk="3" model="myapp.choice">
    <field to="myapp.poll" name="poll" rel="ManyToOneRel">1</field>
    <field type="CharField" name="choice">Pink... no, yellow...</field>
    <field type="IntegerField" name="votes">3</field>
  </object>
  <object pk="4" model="myapp.choice">
    <field to="myapp.poll" name="poll" rel="ManyToOneRel">2</field>
    <field type="CharField" name="choice">Django</field>
    <field type="IntegerField" name="votes">1000000</field>
  </object>
  <object pk="5" model="myapp.choice">
    <field to="myapp.poll" name="poll" rel="ManyToOneRel">2</field>
    <field type="CharField" name="choice">Rails</field>
    <field type="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