| 1 | = Examples of serialized objects = |
| 2 | |
| 3 | Examples of data serialized with Django's [http://www.djangoproject.com/documentation/serialization/ serialization framework]. |
| 4 | |
| 5 | These all use the {{{Poll}}} example from [http://www.djangoproject.com/documentation/tutorial01/ tutorial 1]: |
| 6 | |
| 7 | {{{ |
| 8 | #!python |
| 9 | from django.db import models |
| 10 | |
| 11 | class Poll(models.Model): |
| 12 | question = models.CharField(maxlength=200) |
| 13 | pub_date = models.DateTimeField('date published') |
| 14 | |
| 15 | class Choice(models.Model): |
| 16 | poll = models.ForeignKey(Poll) |
| 17 | choice = models.CharField(maxlength=200) |
| 18 | votes = models.IntegerField() |
| 19 | }}} |
| 20 | |
| 21 | == XML == |
| 22 | |
| 23 | 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: |
| 24 | |
| 25 | {{{ |
| 26 | #!xml |
| 27 | <?xml version="1.0" encoding="utf-8"?> |
| 28 | <django-objects version="1.0"> |
| 29 | <object pk="1" model="myapp.poll"> |
| 30 | <field type="CharField" name="question">What is your favorite color?</field> |
| 31 | <field type="DateTimeField" name="pub_date">2007-06-06 11:23:42</field> |
| 32 | </object> |
| 33 | <object pk="2" model="myapp.poll"> |
| 34 | <field type="CharField" name="question">What's your favorite web framework?</field> |
| 35 | <field type="DateTimeField" name="pub_date">2007-06-06 11:24:17</field> |
| 36 | </object> |
| 37 | <object pk="1" model="myapp.choice"> |
| 38 | <field to="myapp.poll" name="poll" rel="ManyToOneRel">1</field> |
| 39 | <field type="CharField" name="choice">Red</field> |
| 40 | <field type="IntegerField" name="votes">5</field> |
| 41 | </object> |
| 42 | <object pk="2" model="myapp.choice"> |
| 43 | <field to="myapp.poll" name="poll" rel="ManyToOneRel">1</field> |
| 44 | <field type="CharField" name="choice">Blue</field> |
| 45 | <field type="IntegerField" name="votes">1</field> |
| 46 | </object> |
| 47 | <object pk="3" model="myapp.choice"> |
| 48 | <field to="myapp.poll" name="poll" rel="ManyToOneRel">1</field> |
| 49 | <field type="CharField" name="choice">Pink... no, yellow...</field> |
| 50 | <field type="IntegerField" name="votes">3</field> |
| 51 | </object> |
| 52 | <object pk="4" model="myapp.choice"> |
| 53 | <field to="myapp.poll" name="poll" rel="ManyToOneRel">2</field> |
| 54 | <field type="CharField" name="choice">Django</field> |
| 55 | <field type="IntegerField" name="votes">1000000</field> |
| 56 | </object> |
| 57 | <object pk="5" model="myapp.choice"> |
| 58 | <field to="myapp.poll" name="poll" rel="ManyToOneRel">2</field> |
| 59 | <field type="CharField" name="choice">Rails</field> |
| 60 | <field type="IntegerField" name="votes">0</field> |
| 61 | </object> |
| 62 | </django-objects> |
| 63 | }}} |
| 64 | |
| 65 | == JSON == |
| 66 | |
| 67 | Though XML is canonical, [http://json.org 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. |
| 68 | |
| 69 | {{{ |
| 70 | [ |
| 71 | { |
| 72 | "pk": "1", |
| 73 | "model": "se.poll", |
| 74 | "fields": { |
| 75 | "pub_date": "2007-06-06 11:23:42", |
| 76 | "question": "What is your favorite color?" |
| 77 | } |
| 78 | }, |
| 79 | { |
| 80 | "pk": "2", |
| 81 | "model": "se.poll", |
| 82 | "fields": { |
| 83 | "pub_date": "2007-06-06 11:24:17", |
| 84 | "question": "What's your favorite web framework?" |
| 85 | } |
| 86 | }, |
| 87 | { |
| 88 | "pk": "1", |
| 89 | "model": "se.choice", |
| 90 | "fields": { |
| 91 | "votes": 5, |
| 92 | "poll": 1, |
| 93 | "choice": "Red" |
| 94 | } |
| 95 | }, |
| 96 | { |
| 97 | "pk": "2", |
| 98 | "model": "se.choice", |
| 99 | "fields": { |
| 100 | "votes": 1, |
| 101 | "poll": 1, |
| 102 | "choice": "Blue" |
| 103 | } |
| 104 | }, |
| 105 | { |
| 106 | "pk": "3", |
| 107 | "model": "se.choice", |
| 108 | "fields": { |
| 109 | "votes": 3, |
| 110 | "poll": 1, |
| 111 | "choice": "Pink... no, yellow..." |
| 112 | } |
| 113 | }, |
| 114 | { |
| 115 | "pk": "4", |
| 116 | "model": "se.choice", |
| 117 | "fields": { |
| 118 | "votes": 1000000, |
| 119 | "poll": 2, |
| 120 | "choice": "Django" |
| 121 | } |
| 122 | }, |
| 123 | { |
| 124 | "pk": "5", |
| 125 | "model": "se.choice", |
| 126 | "fields": { |
| 127 | "votes": 0, |
| 128 | "poll": 2, |
| 129 | "choice": "Rails" |
| 130 | } |
| 131 | } |
| 132 | ] |
| 133 | }}} |
| 134 | |
| 135 | == YAML == |
| 136 | |
| 137 | Only available if [http://pyyaml.org 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. |
| 138 | |
| 139 | {{{ |
| 140 | - model: se.poll |
| 141 | pk: '1' |
| 142 | fields: |
| 143 | pub_date: !!timestamp '2007-06-06 11:23:42' |
| 144 | question: 'What is your favorite color?' |
| 145 | - model: se.poll |
| 146 | pk: '2' |
| 147 | fields: |
| 148 | pub_date: !!timestamp '2007-06-06 11:24:17' |
| 149 | question: 'What''s your favorite web framework?' |
| 150 | - model: se.choice |
| 151 | pk: '1' |
| 152 | fields: |
| 153 | choice: Red |
| 154 | poll: 1 |
| 155 | votes: 5 |
| 156 | - model: se.choice |
| 157 | pk: '2' |
| 158 | fields: |
| 159 | choice: Blue |
| 160 | poll: 1 |
| 161 | votes: 1 |
| 162 | - model: se.choice |
| 163 | pk: '3' |
| 164 | fields: |
| 165 | choice: 'Pink... no, yellow...' |
| 166 | poll: 1 |
| 167 | votes: 3 |
| 168 | - model: se.choice |
| 169 | pk: '4' |
| 170 | fields: |
| 171 | choice: Django |
| 172 | poll: 2 |
| 173 | votes: 1000000 |
| 174 | - model: se.choice |
| 175 | pk: '5' |
| 176 | fields: |
| 177 | choice: Rails |
| 178 | poll: 2 |
| 179 | votes: 0 |
| 180 | }}} |