Ticket #16364: 16364.patch
File 16364.patch, 3.9 KB (added by , 13 years ago) |
---|
-
docs/topics/serialization.txt
207 207 object, but it can cause difficulty in some circumstances. 208 208 209 209 Consider 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 211 serialize an object that refers to a content type, you need to have a way to 212 refer to that content type. Content types are automatically created by Django 213 as part of the database synchronization process. As a result, the primary key 214 of any given content type isn't easy to predict - it will depend on how and 215 when :djadmin:`syncdb` was executed to create the content types. This is also 216 true for :class:`~django.contrib.auth.models.Permission`. 218 217 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 219 225 There is also the matter of convenience. An integer id isn't always 220 226 the most convenient way to refer to an object; sometimes, a 221 227 more natural reference would be helpful. … … 381 387 ``natural_key()`` methods. You do this by setting a ``dependencies`` 382 388 attribute on the ``natural_key()`` method itself. 383 389 384 For example, consider the ``Permission`` model in ``contrib.auth``.385 The following is a simplified version of the ``Permission`` model::390 For example, let's add a natural key to the ``Book`` model from the 391 example above:: 386 392 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 392 397 def natural_key(self): 393 return (self. codename,) + self.content_type.natural_key()398 return (self.name,) + self.author.natural_key() 394 399 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:: 400 The natural key for a ``Book`` is a combination of its name and of its 401 author. This means that ``Person`` must be serialized before ``Book``. 402 To define this dependency, we add one extra line:: 399 403 400 class Permission(models.Model):401 # ...402 404 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'] 405 407 406 This definition ensures that `` ContentType`` models are serialized before407 `` Permission`` models. In turn, any object referencing ``Permission`` will408 be serialized after both ``ContentType`` and ``Permission``.408 This definition ensures that ``Person`` models are serialized before 409 ``Book`` models. In turn, any object referencing ``Book`` will be 410 serialized after both ``Person`` and ``Book``.