diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt
a
|
b
|
|
197 | 197 | ------------ |
198 | 198 | |
199 | 199 | .. versionadded:: 1.2 |
| 200 | |
200 | 201 | The ability to use natural keys when serializing/deserializing data was |
201 | 202 | added in the 1.2 release. |
202 | 203 | |
… |
… |
|
219 | 220 | the most convenient way to refer to an object; sometimes, a |
220 | 221 | more natural reference would be helpful. |
221 | 222 | |
| 223 | It is for these reasons that Django provides *natural keys*. A natural |
| 224 | key is a tuple of values that can be used to uniquely identify an |
| 225 | object instance without using the primary key value. |
| 226 | |
222 | 227 | Deserialization of natural keys |
223 | 228 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
224 | 229 | |
225 | | It is for these reasons that Django provides `natural keys`. A natural |
226 | | key is a tuple of values that can be used to uniquely identify an |
227 | | object instance without using the primary key value. |
228 | | |
229 | 230 | Consider the following two models:: |
230 | 231 | |
231 | 232 | from django.db import models |
… |
… |
|
236 | 237 | |
237 | 238 | birthdate = models.DateField() |
238 | 239 | |
| 240 | class Meta: |
| 241 | unique_together = (('first_name', 'last_name'),) |
| 242 | |
239 | 243 | class Book(models.Model): |
240 | 244 | name = models.CharField(max_length=100) |
241 | 245 | author = models.ForeignKey(Person) |
… |
… |
|
278 | 282 | |
279 | 283 | birthdate = models.DateField() |
280 | 284 | |
| 285 | class Meta: |
| 286 | unique_together = (('first_name', 'last_name'),) |
| 287 | |
281 | 288 | Now books can use that natural key to refer to ``Person`` objects:: |
282 | 289 | |
283 | 290 | ... |
… |
… |
|
312 | 319 | def natural_key(self): |
313 | 320 | return (self.first_name, self.last_name) |
314 | 321 | |
315 | | Then, when you call ``serializers.serialize()``, you provide a |
316 | | ``use_natural_keys=True`` argument:: |
| 322 | class Meta: |
| 323 | unique_together = (('first_name', 'last_name'),) |
| 324 | |
| 325 | That method should always return a natural key tuple as per our design |
| 326 | (``(first name, last name)`` in this example). Then, when you call |
| 327 | ``serializers.serialize()``, you provide a ``use_natural_keys=True`` argument:: |
317 | 328 | |
318 | 329 | >>> serializers.serialize([book1, book2], format='json', indent=2, use_natural_keys=True) |
319 | 330 | |