~ubuntu-branches/ubuntu/jaunty/python-django/jaunty

« back to all changes in this revision

Viewing changes to django/core/serializers/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
Usage::
5
5
 
6
 
    >>> from django.core import serializers
7
 
    >>> json = serializers.serialize("json", some_query_set)
8
 
    >>> objects = list(serializers.deserialize("json", json))
9
 
        
 
6
    from django.core import serializers
 
7
    json = serializers.serialize("json", some_query_set)
 
8
    objects = list(serializers.deserialize("json", json))
 
9
 
10
10
To add your own serializers, use the SERIALIZATION_MODULES setting::
11
11
 
12
12
    SERIALIZATION_MODULES = {
30
30
    import yaml
31
31
    BUILTIN_SERIALIZERS["yaml"] = "django.core.serializers.pyyaml"
32
32
except ImportError:
33
 
    pass    
 
33
    pass
34
34
 
35
35
_serializers = {}
36
 
        
37
 
def register_serializer(format, serializer_module):
38
 
    """Register a new serializer by passing in a module name."""
 
36
 
 
37
def register_serializer(format, serializer_module, serializers=None):
 
38
    """"Register a new serializer. 
 
39
    
 
40
    ``serializer_module`` should be the fully qualified module name
 
41
    for the serializer.
 
42
    
 
43
    If ``serializers`` is provided, the registration will be added
 
44
    to the provided dictionary.
 
45
    
 
46
    If ``serializers`` is not provided, the registration will be made
 
47
    directly into the global register of serializers. Adding serializers
 
48
    directly is not a thread-safe operation.
 
49
    """
39
50
    module = __import__(serializer_module, {}, {}, [''])
40
 
    _serializers[format] = module
41
 
    
 
51
    if serializers is None:
 
52
        _serializers[format] = module
 
53
    else:
 
54
        serializers[format] = module
 
55
        
42
56
def unregister_serializer(format):
43
 
    """Unregister a given serializer"""
 
57
    "Unregister a given serializer. This is not a thread-safe operation."
44
58
    del _serializers[format]
45
 
    
 
59
 
46
60
def get_serializer(format):
47
61
    if not _serializers:
48
62
        _load_serializers()
52
66
    if not _serializers:
53
67
        _load_serializers()
54
68
    return _serializers.keys()
55
 
    
 
69
 
 
70
def get_public_serializer_formats():
 
71
    if not _serializers:
 
72
        _load_serializers()
 
73
    return [k for k, v in _serializers.iteritems() if not v.Serializer.internal_use_only]
 
74
 
56
75
def get_deserializer(format):
57
76
    if not _serializers:
58
77
        _load_serializers()
59
78
    return _serializers[format].Deserializer
60
 
    
 
79
 
61
80
def serialize(format, queryset, **options):
62
81
    """
63
82
    Serialize a queryset (or any iterator that returns database objects) using
83
102
    that user code has a chance to (e.g.) set up custom settings without
84
103
    needing to be careful of import order.
85
104
    """
 
105
    global _serializers
 
106
    serializers = {}
86
107
    for format in BUILTIN_SERIALIZERS:
87
 
        register_serializer(format, BUILTIN_SERIALIZERS[format])
 
108
        register_serializer(format, BUILTIN_SERIALIZERS[format], serializers)
88
109
    if hasattr(settings, "SERIALIZATION_MODULES"):
89
110
        for format in settings.SERIALIZATION_MODULES:
90
 
            register_serializer(format, settings.SERIALIZATION_MODULES[format])
 
 
b'\\ No newline at end of file'
 
111
            register_serializer(format, settings.SERIALIZATION_MODULES[format], serializers)
 
112
    _serializers = serializers