diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py
index 7afc94c..30bdfe0 100644
a
|
b
|
To add your own serializers, use the SERIALIZATION_MODULES setting::
|
18 | 18 | |
19 | 19 | from django.conf import settings |
20 | 20 | from django.utils import importlib |
| 21 | from django.core.serializers.base import SerializerNotFound |
21 | 22 | |
22 | 23 | # Built-in serializers |
23 | 24 | BUILTIN_SERIALIZERS = { |
… |
… |
def unregister_serializer(format):
|
63 | 64 | del _serializers[format] |
64 | 65 | |
65 | 66 | def get_serializer(format): |
| 67 | if format not in get_serializer_formats(): |
| 68 | raise SerializerNotFound() |
66 | 69 | if not _serializers: |
67 | 70 | _load_serializers() |
68 | 71 | return _serializers[format].Serializer |
diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py
index cdcc7fa..64fc3d7 100644
a
|
b
|
from django.db import models
|
8 | 8 | from django.utils.encoding import smart_str, smart_unicode |
9 | 9 | from django.utils import datetime_safe |
10 | 10 | |
| 11 | class SerializerNotFound(Exception): |
| 12 | """A requested serializer was not found.""" |
| 13 | pass |
| 14 | |
11 | 15 | class SerializationError(Exception): |
12 | 16 | """Something bad happened during serialization.""" |
13 | 17 | pass |
diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt
index c8acc85..80d2885 100644
a
|
b
|
This is useful if you want to serialize data directly to a file-like object
|
38 | 38 | out = open("file.xml", "w") |
39 | 39 | xml_serializer.serialize(SomeModel.objects.all(), stream=out) |
40 | 40 | |
| 41 | .. note:: |
| 42 | |
| 43 | Calling :func:`~django.core.serializers.get_serializer` with an unknown |
| 44 | :ref:`format <serialization-formats>` will raise a |
| 45 | :class:`~django.core.serializers.base.SerializerNotFound` exception. |
| 46 | |
41 | 47 | Subset of fields |
42 | 48 | ~~~~~~~~~~~~~~~~ |
43 | 49 | |
diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
index de57a6f..00d2a23 100644
a
|
b
|
except ImportError:
|
17 | 17 | |
18 | 18 | from django.conf import settings |
19 | 19 | from django.core import serializers, management |
| 20 | from django.core.serializers.base import SerializerNotFound |
20 | 21 | from django.db import transaction, DEFAULT_DB_ALIAS, connection |
21 | 22 | from django.test import TestCase |
22 | 23 | from django.utils.functional import curry |
… |
… |
if connection.features.allows_primary_key_0:
|
350 | 351 | # Dynamically create serializer tests to ensure that all |
351 | 352 | # registered serializers are automatically tested. |
352 | 353 | class SerializerTests(TestCase): |
353 | | pass |
| 354 | def test_get_unknown_serializer(self): |
| 355 | self.assertRaises(SerializerNotFound, |
| 356 | serializers.get_serializer, 'unknown') |
354 | 357 | |
355 | 358 | def serializerTest(format, self): |
356 | 359 | |
… |
… |
for format in serializers.get_serializer_formats():
|
416 | 419 | setattr(SerializerTests, 'test_' + format + '_serializer_fields', curry(fieldsTest, format)) |
417 | 420 | if format != 'python': |
418 | 421 | setattr(SerializerTests, 'test_' + format + '_serializer_stream', curry(streamTest, format)) |
| 422 | |
| 423 | # Regression test for #15889 -- serializers.get_serializer('unknown') used to |
| 424 | # raise a KeyError, should now raise a SerializerNotFound. |
| 425 | class GetSerializerTests(TestCase): |
| 426 | pass |