I do some changes to my previous API: (https://gist.github.com/2597306
<- change are included)
* which fields of object are default serialized. It's depend on
include_default_field but opposite to Tom Christie solution it's default
value is True so all fields (eventually specified in Meta.model_fields)
are present
.
* follow_object attribute. In short - on which object should work
Serializer's child Serializer. Tom wrote about this in previous mail but
I didn't fully understand the problem so I gave him bad answer. It's
better described in algorithm I present.
* get rid of aliases and preserve_field_ordering fields
* change class hierarchy
class Serializer(object) # base class for serializing
class Field(Serializer) # class for serializing fields in objects
class ObjectSerializer(Serializer) # class for serializing objects
class ModelSerializer(Serializer) # class for serializing Django
Models.
I prepare list of steps for first phase of serialization. It's written
in English-Python pseudo code :) Hope indentation will be preserved.
Serializer.serialize is function that for object will return dict with
python native datatypes.
(Object|Model)Serializer.serialize(object, field_name (can be None),
**options)
1. Get object
1.1. if object is iterable then do this algorithm for all elements
and return list of returned values
1.2. if field_name for object is set from upper level we have
object Obj:
1.2.1. if Meta.follow_object == True then work on object
Obj.field_name
1.2.2. else work on Obj
2. Find all fields Fs that should be serialized
2.1. Get all fields declared in Serializer
2.2. Get all fields from Meta.fields
2.3. If Meta.include_default_fields = True then get all fields where
type is valid in Meta.model_fields and not in Meta.exclude
3. Create dictionary A and for F in Fs:
3.1. Find serializer for F
3.1.1. If F is declared in Serializer then serializer is
explicit declared
3.1.2. Else get serializer for F type (m2m related etc)
3.2. Save in dictionary A[field_name] = serializer_value
3.2.1. If field has set label then field_name = label
3.2.2. If field has set attribute=True then add this to
dictionary A[__attributes__][field_name] = serializer_value
4. Return A
Field.serialize(object, field_name (can be None), **options)
1. Get object
1.1. if it is iterable then do this algorithm for all elements
1.2. work on object Obj passed from upper level
2. Find all fields Fs that should be serialized
2.1. Get all fields from declared fields
3. Create dictionary A and for F in Fs:
3.1. Find serializer for F
3.1.1. F is in declared fields so serializer is explicit declared
3.2. Save in dictionary A[field_name] = serializer_value
3.2.1. If field has set label then field_name = label
3.2.2. If field has set attribute=True then add this to
dictionary A[__attributes__][field_name] = serializer_value
4. Resolve function serialized_value
4.1. If Fs (and A) is empty:
4.1.1. If function field_name returns None then return
serialized_value
4.1.2. Else return {field_name() : serialized_value()}
4.2. Else
4.2.1. If function field_name returns None then raise Exception
4.2.2. Else A.update({field_name() : serialized_value()})
5. Return A
We have dict (list of dicts) from first phase of serialization. Next
__attributes__ must be resolve (depends on format and strategy).
Deserialization: (it's early idea)
SomeSerializer.deserialize(D - python_native_datetype_objects (dict or
list of dict), instance=None, field_name=None, class_name=None, **options)
1. Get object instance # Resolving this may be more complicated than I
wrote below (e.g. base on D fields - duck typing)
1.1. If instance is not None then use it
1.2. Else try resolve class_name
1.2.1. If class_name is class object instantiate it.
1.2.2. If class_name is string then find string value for this
key in D and instantiate it
1.2.3. If class_name is None raise Exception
2. Find all fields in D and find fields in Serializer for deserializing them
2.1. Resolve label attribute for fields
3. Pass instance, data D and field_name to all fields Serializers
4. Return instance
I'm aware that there will be lot of small issues but I believe that
ideas are good.
--
Piotr Grabowski
--
You received this message because you are subscribed to the Google Groups "Django
developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en.