Ticket #13418: 13418-r13135.diff

File 13418-r13135.diff, 2.0 KB (added by Ramiro Morales, 14 years ago)
  • docs/topics/serialization.txt

    diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt
    a b  
    197197------------
    198198
    199199.. versionadded:: 1.2
     200
    200201   The ability to use natural keys when serializing/deserializing data was
    201202   added in the 1.2 release.
    202203
     
    219220the most convenient way to refer to an object; sometimes, a
    220221more natural reference would be helpful.
    221222
     223It is for these reasons that Django provides *natural keys*. A natural
     224key is a tuple of values that can be used to uniquely identify an
     225object instance without using the primary key value.
     226
    222227Deserialization of natural keys
    223228~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    224229
    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 
    229230Consider the following two models::
    230231
    231232    from django.db import models
     
    236237
    237238        birthdate = models.DateField()
    238239
     240        class Meta:
     241            unique_together = (('first_name', 'last_name'),)
     242
    239243    class Book(models.Model):
    240244        name = models.CharField(max_length=100)
    241245        author = models.ForeignKey(Person)
     
    278282
    279283        birthdate = models.DateField()
    280284
     285        class Meta:
     286            unique_together = (('first_name', 'last_name'),)
     287
    281288Now books can use that natural key to refer to ``Person`` objects::
    282289
    283290    ...
     
    312319        def natural_key(self):
    313320            return (self.first_name, self.last_name)
    314321
    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
     325That 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::
    317328
    318329    >>> serializers.serialize([book1, book2], format='json', indent=2, use_natural_keys=True)
    319330
Back to Top