Ticket #16364: 16364.patch

File 16364.patch, 3.9 KB (added by aaugustin, 4 years ago)
  • docs/topics/serialization.txt

     
    207207object, but it can cause difficulty in some circumstances.
    208208
    209209Consider the case of a list of objects that have foreign key on
    210 :class:`ContentType`. If you're going to serialize an object that
    211 refers to a content type, you need to have a way to refer to that
    212 content type. Content Types are automatically created by Django as
    213 part of the database synchronization process, so you don't need to
    214 include content types in a fixture or other serialized data. As a
    215 result, the primary key of any given content type isn't easy to
    216 predict - it will depend on how and when :djadmin:`syncdb` was
    217 executed to create the content types.
     210:class:`~django.contrib.conttenttypes.models.ContentType`. If you're going to
     211serialize an object that refers to a content type, you need to have a way to
     212refer to that content type. Content types are automatically created by Django
     213as part of the database synchronization process. As a result, the primary key
     214of any given content type isn't easy to predict - it will depend on how and
     215when :djadmin:`syncdb` was executed to create the content types. This is also
     216true for :class:`~django.contrib.auth.models.Permission`.
    218217
     218.. warning::
     219
     220    You should never include automatically created objects in a fixture or
     221    other serialized data. If the primary keys in the fixture and in the
     222    database match by chance, loading the fixture will have no effect. If they
     223    don't, you're likely to encounter a :class:`django.db.IntegrityError`.
     224
    219225There is also the matter of convenience. An integer id isn't always
    220226the most convenient way to refer to an object; sometimes, a
    221227more natural reference would be helpful.
     
    381387``natural_key()`` methods. You do this by setting a ``dependencies``
    382388attribute on the ``natural_key()`` method itself.
    383389
    384 For example, consider the ``Permission`` model in ``contrib.auth``.
    385 The following is a simplified version of the ``Permission`` model::
     390For example, let's add a natural key to the ``Book`` model from the
     391example above::
    386392
    387     class Permission(models.Model):
    388         name = models.CharField(max_length=50)
    389         content_type = models.ForeignKey(ContentType)
    390         codename = models.CharField(max_length=100)
    391         # ...
     393    class Book(models.Model):
     394        name = models.CharField(max_length=100)
     395        author = models.ForeignKey(Person)
     396
    392397        def natural_key(self):
    393             return (self.codename,) + self.content_type.natural_key()
     398            return (self.name,) + self.author.natural_key()
    394399
    395 The natural key for a ``Permission`` is a combination of the codename for the
    396 ``Permission``, and the ``ContentType`` to which the ``Permission`` applies. This means
    397 that ``ContentType`` must be serialized before ``Permission``. To define this
    398 dependency, we add one extra line::
     400The natural key for a ``Book`` is a combination of its name and of its
     401author.  This means that ``Person`` must be serialized before ``Book``.
     402To define this dependency, we add one extra line::
    399403
    400     class Permission(models.Model):
    401         # ...
    402404        def natural_key(self):
    403             return (self.codename,) + self.content_type.natural_key()
    404         natural_key.dependencies = ['contenttypes.contenttype']
     405            return (self.name,) + self.author.natural_key()
     406        natural_key.dependencies = ['example_app.person']
    405407
    406 This definition ensures that ``ContentType`` models are serialized before
    407 ``Permission`` models. In turn, any object referencing ``Permission`` will
    408 be serialized after both ``ContentType`` and ``Permission``.
     408This definition ensures that ``Person`` models are serialized before
     409``Book`` models. In turn, any object referencing ``Book`` will be
     410serialized after both ``Person`` and ``Book``.
Back to Top