2006-06-29 00:00:37 +08:00
|
|
|
==========================
|
|
|
|
Serializing Django objects
|
|
|
|
==========================
|
|
|
|
|
|
|
|
Django's serialization framework provides a mechanism for "translating" Django
|
2012-05-03 14:57:22 +08:00
|
|
|
models into other formats. Usually these other formats will be text-based and
|
|
|
|
used for sending Django data over a wire, but it's possible for a
|
2006-06-29 00:00:37 +08:00
|
|
|
serializer to handle any format (text-based or not).
|
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
If you just want to get some data from your tables into a serialized
|
|
|
|
form, you could use the :djadmin:`dumpdata` management command.
|
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
Serializing data
|
|
|
|
----------------
|
|
|
|
|
|
|
|
At the highest level, serializing data is a very simple operation::
|
|
|
|
|
|
|
|
from django.core import serializers
|
|
|
|
data = serializers.serialize("xml", SomeModel.objects.all())
|
2006-09-26 01:44:07 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
The arguments to the ``serialize`` function are the format to serialize the data
|
2011-09-30 18:28:39 +08:00
|
|
|
to (see `Serialization formats`_) and a
|
|
|
|
:class:`~django.db.models.query.QuerySet` to serialize. (Actually, the second
|
2012-05-03 14:57:22 +08:00
|
|
|
argument can be any iterator that yields Django model instances, but it'll
|
|
|
|
almost always be a QuerySet).
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
.. function:: django.core.serializers.get_serializer(format)
|
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
You can also use a serializer object directly::
|
|
|
|
|
2007-03-22 20:54:23 +08:00
|
|
|
XMLSerializer = serializers.get_serializer("xml")
|
|
|
|
xml_serializer = XMLSerializer()
|
2006-06-29 00:00:37 +08:00
|
|
|
xml_serializer.serialize(queryset)
|
|
|
|
data = xml_serializer.getvalue()
|
2006-09-26 01:44:07 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
This is useful if you want to serialize data directly to a file-like object
|
2008-11-02 04:14:36 +08:00
|
|
|
(which includes an :class:`~django.http.HttpResponse`)::
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2012-05-05 20:01:38 +08:00
|
|
|
with open("file.xml", "w") as out:
|
|
|
|
xml_serializer.serialize(SomeModel.objects.all(), stream=out)
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2011-04-27 00:49:32 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Calling :func:`~django.core.serializers.get_serializer` with an unknown
|
|
|
|
:ref:`format <serialization-formats>` will raise a
|
2013-01-01 21:12:42 +08:00
|
|
|
``django.core.serializers.SerializerDoesNotExist`` exception.
|
2011-04-27 00:49:32 +08:00
|
|
|
|
2014-11-09 00:08:12 +08:00
|
|
|
.. _subset-of-fields:
|
|
|
|
|
2007-06-01 21:39:08 +08:00
|
|
|
Subset of fields
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
2007-11-04 10:08:24 +08:00
|
|
|
If you only want a subset of fields to be serialized, you can
|
2007-07-01 09:00:23 +08:00
|
|
|
specify a ``fields`` argument to the serializer::
|
2007-06-01 21:39:08 +08:00
|
|
|
|
|
|
|
from django.core import serializers
|
|
|
|
data = serializers.serialize('xml', SomeModel.objects.all(), fields=('name','size'))
|
|
|
|
|
2007-07-01 09:00:23 +08:00
|
|
|
In this example, only the ``name`` and ``size`` attributes of each model will
|
2007-11-04 10:08:24 +08:00
|
|
|
be serialized.
|
2007-06-01 21:39:08 +08:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2008-08-04 05:02:59 +08:00
|
|
|
Depending on your model, you may find that it is not possible to
|
|
|
|
deserialize a model that only serializes a subset of its fields. If a
|
|
|
|
serialized object doesn't specify all the fields that are required by a
|
|
|
|
model, the deserializer will not be able to save deserialized instances.
|
2007-06-01 21:39:08 +08:00
|
|
|
|
2008-06-09 22:03:35 +08:00
|
|
|
Inherited Models
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
2008-09-04 04:23:18 +08:00
|
|
|
If you have a model that is defined using an :ref:`abstract base class
|
|
|
|
<abstract-base-classes>`, you don't have to do anything special to serialize
|
|
|
|
that model. Just call the serializer on the object (or objects) that you want to
|
|
|
|
serialize, and the output will be a complete representation of the serialized
|
|
|
|
object.
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2008-09-04 04:23:18 +08:00
|
|
|
However, if you have a model that uses :ref:`multi-table inheritance
|
|
|
|
<multi-table-inheritance>`, you also need to serialize all of the base classes
|
|
|
|
for the model. This is because only the fields that are locally defined on the
|
|
|
|
model will be serialized. For example, consider the following models::
|
2008-08-04 05:02:59 +08:00
|
|
|
|
2008-09-04 04:23:18 +08:00
|
|
|
class Place(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
2008-08-04 05:02:59 +08:00
|
|
|
|
2008-09-04 04:23:18 +08:00
|
|
|
class Restaurant(Place):
|
2014-08-14 00:33:06 +08:00
|
|
|
serves_hot_dogs = models.BooleanField(default=False)
|
2008-08-04 05:02:59 +08:00
|
|
|
|
2008-06-09 22:03:35 +08:00
|
|
|
If you only serialize the Restaurant model::
|
|
|
|
|
2008-09-04 04:23:18 +08:00
|
|
|
data = serializers.serialize('xml', Restaurant.objects.all())
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2013-03-22 17:50:45 +08:00
|
|
|
the fields on the serialized output will only contain the ``serves_hot_dogs``
|
|
|
|
attribute. The ``name`` attribute of the base class will be ignored.
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2013-03-22 17:50:45 +08:00
|
|
|
In order to fully serialize your ``Restaurant`` instances, you will need to
|
|
|
|
serialize the ``Place`` models as well::
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2008-09-04 04:23:18 +08:00
|
|
|
all_objects = list(Restaurant.objects.all()) + list(Place.objects.all())
|
|
|
|
data = serializers.serialize('xml', all_objects)
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
Deserializing data
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Deserializing data is also a fairly simple operation::
|
|
|
|
|
|
|
|
for obj in serializers.deserialize("xml", data):
|
|
|
|
do_something_with(obj)
|
2006-09-26 01:44:07 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
As you can see, the ``deserialize`` function takes the same format argument as
|
|
|
|
``serialize``, a string or stream of data, and returns an iterator.
|
|
|
|
|
|
|
|
However, here it gets slightly complicated. The objects returned by the
|
|
|
|
``deserialize`` iterator *aren't* simple Django objects. Instead, they are
|
|
|
|
special ``DeserializedObject`` instances that wrap a created -- but unsaved --
|
|
|
|
object and any associated relationship data.
|
|
|
|
|
|
|
|
Calling ``DeserializedObject.save()`` saves the object to the database.
|
|
|
|
|
2013-02-12 14:00:38 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
If the ``pk`` attribute in the serialized data doesn't exist or is
|
|
|
|
null, a new instance will be saved to the database.
|
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
This ensures that deserializing is a non-destructive operation even if the
|
|
|
|
data in your serialized representation doesn't match what's currently in the
|
|
|
|
database. Usually, working with these ``DeserializedObject`` instances looks
|
|
|
|
something like::
|
|
|
|
|
|
|
|
for deserialized_object in serializers.deserialize("xml", data):
|
|
|
|
if object_should_be_saved(deserialized_object):
|
2008-02-19 07:43:12 +08:00
|
|
|
deserialized_object.save()
|
2006-09-26 01:44:07 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
In other words, the usual use is to examine the deserialized objects to make
|
2008-08-04 05:02:59 +08:00
|
|
|
sure that they are "appropriate" for saving before doing so. Of course, if you
|
|
|
|
trust your data source you could just save the object and move on.
|
2006-06-29 00:00:37 +08:00
|
|
|
|
|
|
|
The Django object itself can be inspected as ``deserialized_object.object``.
|
2013-08-19 20:29:32 +08:00
|
|
|
If fields in the serialized data do not exist on a model, a
|
|
|
|
``DeserializationError`` will be raised unless the ``ignorenonexistent``
|
|
|
|
argument is passed in as ``True``::
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2013-08-19 20:29:32 +08:00
|
|
|
serializers.deserialize("xml", data, ignorenonexistent=True)
|
2012-09-30 20:34:13 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _serialization-formats:
|
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
Serialization formats
|
|
|
|
---------------------
|
|
|
|
|
2009-02-21 16:40:07 +08:00
|
|
|
Django supports a number of serialization formats, some of which require you
|
|
|
|
to install third-party Python modules:
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
========== ==============================================================
|
|
|
|
Identifier Information
|
|
|
|
========== ==============================================================
|
|
|
|
``xml`` Serializes to and from a simple XML dialect.
|
2006-06-30 00:42:49 +08:00
|
|
|
|
2012-04-30 01:58:00 +08:00
|
|
|
``json`` Serializes to and from JSON_.
|
2006-06-30 00:42:49 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``yaml`` Serializes to YAML (YAML Ain't a Markup Language). This
|
|
|
|
serializer is only available if PyYAML_ is installed.
|
|
|
|
========== ==============================================================
|
2006-06-30 00:42:49 +08:00
|
|
|
|
|
|
|
.. _json: http://json.org/
|
2007-06-01 21:39:08 +08:00
|
|
|
.. _PyYAML: http://www.pyyaml.org/
|
2006-06-30 00:42:49 +08:00
|
|
|
|
2013-02-24 02:11:56 +08:00
|
|
|
XML
|
|
|
|
~~~
|
|
|
|
|
|
|
|
The basic XML serialization format is quite simple::
|
2013-03-22 17:50:45 +08:00
|
|
|
|
2013-02-24 02:11:56 +08:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<django-objects version="1.0">
|
|
|
|
<object pk="123" model="sessions.session">
|
|
|
|
<field type="DateTimeField" name="expire_date">2013-01-16T08:16:59.844560+00:00</field>
|
|
|
|
<!-- ... -->
|
|
|
|
</object>
|
|
|
|
</django-objects>
|
|
|
|
|
|
|
|
The whole collection of objects that is either serialized or de-serialized is
|
|
|
|
represented by a ``<django-objects>``-tag which contains multiple
|
|
|
|
``<object>``-elements. Each such object has two attributes: "pk" and "model",
|
|
|
|
the latter being represented by the name of the app ("sessions") and the
|
|
|
|
lowercase name of the model ("session") separated by a dot.
|
|
|
|
|
|
|
|
Each field of the object is serialized as a ``<field>``-element sporting the
|
|
|
|
fields "type" and "name". The text content of the element represents the value
|
|
|
|
that should be stored.
|
|
|
|
|
|
|
|
Foreign keys and other relational fields are treated a little bit differently::
|
2013-03-22 17:50:45 +08:00
|
|
|
|
2013-02-24 02:11:56 +08:00
|
|
|
<object pk="27" model="auth.permission">
|
|
|
|
<!-- ... -->
|
|
|
|
<field to="contenttypes.contenttype" name="content_type" rel="ManyToOneRel">9</field>
|
|
|
|
<!-- ... -->
|
|
|
|
</object>
|
|
|
|
|
2013-05-16 15:21:18 +08:00
|
|
|
In this example we specify that the auth.Permission object with the PK 27 has
|
2013-02-24 02:11:56 +08:00
|
|
|
a foreign key to the contenttypes.ContentType instance with the PK 9.
|
|
|
|
|
|
|
|
ManyToMany-relations are exported for the model that binds them. For instance,
|
|
|
|
the auth.User model has such a relation to the auth.Permission model::
|
2013-03-22 17:50:45 +08:00
|
|
|
|
2013-02-24 02:11:56 +08:00
|
|
|
<object pk="1" model="auth.user">
|
|
|
|
<!-- ... -->
|
|
|
|
<field to="auth.permission" name="user_permissions" rel="ManyToManyRel">
|
|
|
|
<object pk="46"></object>
|
|
|
|
<object pk="47"></object>
|
|
|
|
</field>
|
|
|
|
</object>
|
|
|
|
|
|
|
|
This example links the given user with the permission models with PKs 46 and 47.
|
|
|
|
|
2014-02-15 01:28:51 +08:00
|
|
|
.. _serialization-formats-json:
|
|
|
|
|
2013-02-24 02:11:56 +08:00
|
|
|
JSON
|
|
|
|
~~~~
|
|
|
|
|
|
|
|
When staying with the same example data as before it would be serialized as
|
|
|
|
JSON in the following way::
|
2013-03-22 17:50:45 +08:00
|
|
|
|
2013-02-24 02:11:56 +08:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"pk": "4b678b301dfd8a4e0dad910de3ae245b",
|
|
|
|
"model": "sessions.session",
|
|
|
|
"fields": {
|
|
|
|
"expire_date": "2013-01-16T08:16:59.844Z",
|
|
|
|
...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
The formatting here is a bit simpler than with XML. The whole collection
|
|
|
|
is just represented as an array and the objects are represented by JSON objects
|
|
|
|
with three properties: "pk", "model" and "fields". "fields" is again an object
|
|
|
|
containing each field's name and value as property and property-value
|
|
|
|
respectively.
|
|
|
|
|
2013-03-22 17:50:45 +08:00
|
|
|
Foreign keys just have the PK of the linked object as property value.
|
2013-02-24 02:11:56 +08:00
|
|
|
ManyToMany-relations are serialized for the model that defines them and are
|
|
|
|
represented as a list of PKs.
|
|
|
|
|
|
|
|
Date and datetime related types are treated in a special way by the JSON
|
|
|
|
serializer to make the format compatible with `ECMA-262`_.
|
2006-09-22 21:26:07 +08:00
|
|
|
|
2012-04-30 01:58:00 +08:00
|
|
|
Be aware that not all Django output can be passed unmodified to :mod:`json`.
|
|
|
|
In particular, :ref:`lazy translation objects <lazy-translations>` need a
|
|
|
|
`special encoder`_ written for them. Something like this will work::
|
2007-11-04 10:08:24 +08:00
|
|
|
|
|
|
|
from django.utils.functional import Promise
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import force_text
|
2013-02-24 02:11:56 +08:00
|
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
2007-11-04 10:08:24 +08:00
|
|
|
|
2013-02-24 02:11:56 +08:00
|
|
|
class LazyEncoder(DjangoJSONEncoder):
|
2007-11-04 10:08:24 +08:00
|
|
|
def default(self, obj):
|
|
|
|
if isinstance(obj, Promise):
|
2012-07-21 16:00:10 +08:00
|
|
|
return force_text(obj)
|
2009-05-02 15:39:45 +08:00
|
|
|
return super(LazyEncoder, self).default(obj)
|
2007-11-04 10:08:24 +08:00
|
|
|
|
2014-11-09 00:08:12 +08:00
|
|
|
Also note that GeoDjango provides a :doc:`customized GeoJSON serializer
|
|
|
|
</ref/contrib/gis/serializers>`.
|
|
|
|
|
2014-12-19 23:25:57 +08:00
|
|
|
.. _special encoder: https://docs.python.org/library/json.html#encoders-and-decoders
|
2013-02-24 02:11:56 +08:00
|
|
|
.. _ecma-262: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
|
|
|
|
|
|
|
|
YAML
|
|
|
|
~~~~
|
|
|
|
|
|
|
|
YAML serialization looks quite similar to JSON. The object list is serialized
|
|
|
|
as a sequence mappings with the keys "pk", "model" and "fields". Each field is
|
|
|
|
again a mapping with the key being name of the field and the value the value::
|
2013-03-22 17:50:45 +08:00
|
|
|
|
2013-02-24 02:11:56 +08:00
|
|
|
- fields: {expire_date: !!timestamp '2013-01-16 08:16:59.844560+00:00'}
|
|
|
|
model: sessions.session
|
|
|
|
pk: 4b678b301dfd8a4e0dad910de3ae245b
|
|
|
|
|
|
|
|
Referential fields are again just represented by the PK or sequence of PKs.
|
2009-01-06 13:13:02 +08:00
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
.. _topics-serialization-natural-keys:
|
|
|
|
|
|
|
|
Natural keys
|
|
|
|
------------
|
|
|
|
|
2012-01-08 05:52:07 +08:00
|
|
|
The default serialization strategy for foreign keys and many-to-many relations
|
|
|
|
is to serialize the value of the primary key(s) of the objects in the relation.
|
|
|
|
This strategy works well for most objects, but it can cause difficulty in some
|
|
|
|
circumstances.
|
|
|
|
|
|
|
|
Consider the case of a list of objects that have a foreign key referencing
|
2012-12-25 22:56:22 +08:00
|
|
|
:class:`~django.contrib.contenttypes.models.ContentType`. If you're going to
|
2012-01-08 05:52:07 +08:00
|
|
|
serialize an object that refers to a content type, then you need to have a way
|
|
|
|
to refer to that content type to begin with. Since ``ContentType`` objects are
|
|
|
|
automatically created by Django during the database synchronization process,
|
|
|
|
the primary key of a given content type isn't easy to predict; it will
|
2013-07-25 23:19:36 +08:00
|
|
|
depend on how and when :djadmin:`migrate` was executed. This is true for all
|
2012-01-08 05:52:07 +08:00
|
|
|
models which automatically generate objects, notably including
|
2012-02-04 20:48:01 +08:00
|
|
|
:class:`~django.contrib.auth.models.Permission`,
|
|
|
|
:class:`~django.contrib.auth.models.Group`, and
|
|
|
|
:class:`~django.contrib.auth.models.User`.
|
2012-01-08 05:52:07 +08:00
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
You should never include automatically generated objects in a fixture or
|
|
|
|
other serialized data. By chance, the primary keys in the fixture
|
|
|
|
may match those in the database and loading the fixture will
|
|
|
|
have no effect. In the more likely case that they don't match, the fixture
|
|
|
|
loading will fail with an :class:`~django.db.IntegrityError`.
|
2009-12-14 20:39:20 +08:00
|
|
|
|
|
|
|
There is also the matter of convenience. An integer id isn't always
|
|
|
|
the most convenient way to refer to an object; sometimes, a
|
|
|
|
more natural reference would be helpful.
|
|
|
|
|
2010-05-09 12:24:58 +08:00
|
|
|
It is for these reasons that Django provides *natural keys*. A natural
|
2009-12-14 20:39:20 +08:00
|
|
|
key is a tuple of values that can be used to uniquely identify an
|
|
|
|
object instance without using the primary key value.
|
|
|
|
|
2010-05-09 12:24:58 +08:00
|
|
|
Deserialization of natural keys
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
Consider the following two models::
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Person(models.Model):
|
|
|
|
first_name = models.CharField(max_length=100)
|
|
|
|
last_name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
birthdate = models.DateField()
|
|
|
|
|
2010-05-09 12:24:58 +08:00
|
|
|
class Meta:
|
|
|
|
unique_together = (('first_name', 'last_name'),)
|
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
class Book(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
author = models.ForeignKey(Person)
|
|
|
|
|
|
|
|
Ordinarily, serialized data for ``Book`` would use an integer to refer to
|
|
|
|
the author. For example, in JSON, a Book might be serialized as::
|
|
|
|
|
|
|
|
...
|
|
|
|
{
|
|
|
|
"pk": 1,
|
|
|
|
"model": "store.book",
|
|
|
|
"fields": {
|
|
|
|
"name": "Mostly Harmless",
|
|
|
|
"author": 42
|
|
|
|
}
|
|
|
|
}
|
|
|
|
...
|
|
|
|
|
|
|
|
This isn't a particularly natural way to refer to an author. It
|
|
|
|
requires that you know the primary key value for the author; it also
|
|
|
|
requires that this primary key value is stable and predictable.
|
|
|
|
|
|
|
|
However, if we add natural key handling to Person, the fixture becomes
|
|
|
|
much more humane. To add natural key handling, you define a default
|
|
|
|
Manager for Person with a ``get_by_natural_key()`` method. In the case
|
|
|
|
of a Person, a good natural key might be the pair of first and last
|
|
|
|
name::
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class PersonManager(models.Manager):
|
|
|
|
def get_by_natural_key(self, first_name, last_name):
|
2009-12-18 08:32:10 +08:00
|
|
|
return self.get(first_name=first_name, last_name=last_name)
|
2009-12-14 20:39:20 +08:00
|
|
|
|
|
|
|
class Person(models.Model):
|
|
|
|
objects = PersonManager()
|
|
|
|
|
|
|
|
first_name = models.CharField(max_length=100)
|
|
|
|
last_name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
birthdate = models.DateField()
|
|
|
|
|
2010-05-09 12:24:58 +08:00
|
|
|
class Meta:
|
|
|
|
unique_together = (('first_name', 'last_name'),)
|
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
Now books can use that natural key to refer to ``Person`` objects::
|
|
|
|
|
|
|
|
...
|
|
|
|
{
|
|
|
|
"pk": 1,
|
|
|
|
"model": "store.book",
|
|
|
|
"fields": {
|
|
|
|
"name": "Mostly Harmless",
|
|
|
|
"author": ["Douglas", "Adams"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
...
|
|
|
|
|
|
|
|
When you try to load this serialized data, Django will use the
|
|
|
|
``get_by_natural_key()`` method to resolve ``["Douglas", "Adams"]``
|
|
|
|
into the primary key of an actual ``Person`` object.
|
|
|
|
|
2010-05-09 12:24:58 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Whatever fields you use for a natural key must be able to uniquely
|
|
|
|
identify an object. This will usually mean that your model will
|
|
|
|
have a uniqueness clause (either unique=True on a single field, or
|
|
|
|
``unique_together`` over multiple fields) for the field or fields
|
|
|
|
in your natural key. However, uniqueness doesn't need to be
|
|
|
|
enforced at the database level. If you are certain that a set of
|
|
|
|
fields will be effectively unique, you can still use those fields
|
|
|
|
as a natural key.
|
|
|
|
|
2012-08-01 09:49:01 +08:00
|
|
|
.. versionadded:: 1.7
|
|
|
|
|
|
|
|
Deserialization of objects with no primary key will always check whether the
|
|
|
|
model's manager has a ``get_by_natural_key()`` method and if so, use it to
|
|
|
|
populate the deserialized object's primary key.
|
|
|
|
|
2009-12-14 20:39:20 +08:00
|
|
|
Serialization of natural keys
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
So how do you get Django to emit a natural key when serializing an object?
|
|
|
|
Firstly, you need to add another method -- this time to the model itself::
|
|
|
|
|
|
|
|
class Person(models.Model):
|
|
|
|
objects = PersonManager()
|
|
|
|
|
|
|
|
first_name = models.CharField(max_length=100)
|
|
|
|
last_name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
birthdate = models.DateField()
|
|
|
|
|
|
|
|
def natural_key(self):
|
|
|
|
return (self.first_name, self.last_name)
|
|
|
|
|
2010-05-09 12:24:58 +08:00
|
|
|
class Meta:
|
|
|
|
unique_together = (('first_name', 'last_name'),)
|
|
|
|
|
|
|
|
That method should always return a natural key tuple -- in this
|
|
|
|
example, ``(first name, last name)``. Then, when you call
|
2012-08-01 09:49:01 +08:00
|
|
|
``serializers.serialize()``, you provide ``use_natural_foreign_keys=True``
|
|
|
|
or ``use_natural_primary_keys=True`` arguments::
|
|
|
|
|
|
|
|
>>> serializers.serialize('json', [book1, book2], indent=2,
|
|
|
|
... use_natural_foreign_keys=True, use_natural_primary_keys=True)
|
|
|
|
|
|
|
|
When ``use_natural_foreign_keys=True`` is specified, Django will use the
|
|
|
|
``natural_key()`` method to serialize any foreign key reference to objects
|
|
|
|
of the type that defines the method.
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2012-08-01 09:49:01 +08:00
|
|
|
When ``use_natural_primary_keys=True`` is specified, Django will not provide the
|
|
|
|
primary key in the serialized data of this object since it can be calculated
|
|
|
|
during deserialization::
|
|
|
|
|
|
|
|
...
|
|
|
|
{
|
|
|
|
"model": "store.person",
|
|
|
|
"fields": {
|
|
|
|
"first_name": "Douglas",
|
|
|
|
"last_name": "Adams",
|
|
|
|
"birth_date": "1952-03-11",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
...
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2012-08-01 09:49:01 +08:00
|
|
|
This can be useful when you need to load serialized data into an existing
|
|
|
|
database and you cannot guarantee that the serialized primary key value is not
|
|
|
|
already in use, and do not need to ensure that deserialized objects retain the
|
|
|
|
same primary keys.
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2012-08-01 09:49:01 +08:00
|
|
|
If you are using :djadmin:`dumpdata` to generate serialized data, use the
|
|
|
|
:djadminopt:`--natural-foreign` and :djadminopt:`--natural-primary` command
|
|
|
|
line flags to generate natural keys.
|
2009-12-14 20:39:20 +08:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
You don't need to define both ``natural_key()`` and
|
|
|
|
``get_by_natural_key()``. If you don't want Django to output
|
|
|
|
natural keys during serialization, but you want to retain the
|
|
|
|
ability to load natural keys, then you can opt to not implement
|
|
|
|
the ``natural_key()`` method.
|
|
|
|
|
|
|
|
Conversely, if (for some strange reason) you want Django to output
|
|
|
|
natural keys during serialization, but *not* be able to load those
|
|
|
|
key values, just don't define the ``get_by_natural_key()`` method.
|
|
|
|
|
|
|
|
Dependencies during serialization
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Since natural keys rely on database lookups to resolve references, it
|
2012-01-08 05:52:07 +08:00
|
|
|
is important that the data exists before it is referenced. You can't make
|
2013-03-22 17:50:45 +08:00
|
|
|
a "forward reference" with natural keys -- the data you're referencing
|
2009-12-14 20:39:20 +08:00
|
|
|
must exist before you include a natural key reference to that data.
|
|
|
|
|
|
|
|
To accommodate this limitation, calls to :djadmin:`dumpdata` that use
|
2012-08-01 09:49:01 +08:00
|
|
|
the :djadminopt:`--natural-foreign` option will serialize any model with a
|
2012-01-08 05:52:07 +08:00
|
|
|
``natural_key()`` method before serializing standard primary key objects.
|
2009-12-14 20:39:20 +08:00
|
|
|
|
|
|
|
However, this may not always be enough. If your natural key refers to
|
|
|
|
another object (by using a foreign key or natural key to another object
|
|
|
|
as part of a natural key), then you need to be able to ensure that
|
|
|
|
the objects on which a natural key depends occur in the serialized data
|
|
|
|
before the natural key requires them.
|
|
|
|
|
|
|
|
To control this ordering, you can define dependencies on your
|
|
|
|
``natural_key()`` methods. You do this by setting a ``dependencies``
|
|
|
|
attribute on the ``natural_key()`` method itself.
|
|
|
|
|
2012-01-08 05:52:07 +08:00
|
|
|
For example, let's add a natural key to the ``Book`` model from the
|
|
|
|
example above::
|
|
|
|
|
|
|
|
class Book(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
author = models.ForeignKey(Person)
|
2009-12-14 20:39:20 +08:00
|
|
|
|
|
|
|
def natural_key(self):
|
2012-01-08 05:52:07 +08:00
|
|
|
return (self.name,) + self.author.natural_key()
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2012-01-08 05:52:07 +08:00
|
|
|
The natural key for a ``Book`` is a combination of its name and its
|
2012-02-04 04:16:06 +08:00
|
|
|
author. This means that ``Person`` must be serialized before ``Book``.
|
2012-01-08 05:52:07 +08:00
|
|
|
To define this dependency, we add one extra line::
|
2009-12-14 20:39:20 +08:00
|
|
|
|
|
|
|
def natural_key(self):
|
2012-01-08 05:52:07 +08:00
|
|
|
return (self.name,) + self.author.natural_key()
|
|
|
|
natural_key.dependencies = ['example_app.person']
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2012-01-08 05:52:07 +08:00
|
|
|
This definition ensures that all ``Person`` objects are serialized before
|
|
|
|
any ``Book`` objects. In turn, any object referencing ``Book`` will be
|
|
|
|
serialized after both ``Person`` and ``Book`` have been serialized.
|