Commit b72a99fe by Tom Christie

Merge branch 'display-nested-data' into html-form-renderer

parents 0966a268 e03854ba
...@@ -214,8 +214,6 @@ Nested relationships can be expressed by using serializers as fields. ...@@ -214,8 +214,6 @@ Nested relationships can be expressed by using serializers as fields.
If the field is used to represent a to-many relationship, you should add the `many=True` flag to the serializer field. If the field is used to represent a to-many relationship, you should add the `many=True` flag to the serializer field.
Note that nested relationships are currently read-only. For read-write relationships, you should use a flat relational style.
## Example ## Example
For example, the following serializer: For example, the following serializer:
......
...@@ -184,7 +184,7 @@ If a nested representation may optionally accept the `None` value you should pas ...@@ -184,7 +184,7 @@ If a nested representation may optionally accept the `None` value you should pas
content = serializers.CharField(max_length=200) content = serializers.CharField(max_length=200)
created = serializers.DateTimeField() created = serializers.DateTimeField()
Similarly if a nested representation should be a list of items, you should the `many=True` flag to the nested serialized. Similarly if a nested representation should be a list of items, you should pass the `many=True` flag to the nested serialized.
class CommentSerializer(serializers.Serializer): class CommentSerializer(serializers.Serializer):
user = UserSerializer(required=False) user = UserSerializer(required=False)
...@@ -192,11 +192,13 @@ Similarly if a nested representation should be a list of items, you should the ` ...@@ -192,11 +192,13 @@ Similarly if a nested representation should be a list of items, you should the `
content = serializers.CharField(max_length=200) content = serializers.CharField(max_length=200)
created = serializers.DateTimeField() created = serializers.DateTimeField()
--- Validation of nested objects will work the same as before. Errors with nested objects will be nested under the field name of the nested object.
**Note**: Nested serializers are only suitable for read-only representations, as there are cases where they would have ambiguous or non-obvious behavior if used when updating instances. For read-write representations you should always use a flat representation, by using one of the `RelatedField` subclasses.
--- serializer = CommentSerializer(comment, data={'user': {'email': 'foobar', 'username': 'doe'}, 'content': 'baz'})
serializer.is_valid()
# False
serializer.errors
# {'user': {'email': [u'Enter a valid e-mail address.']}, 'created': [u'This field is required.']}
## Dealing with multiple objects ## Dealing with multiple objects
...@@ -260,7 +262,7 @@ When performing a bulk update you may want to allow new items to be created, and ...@@ -260,7 +262,7 @@ When performing a bulk update you may want to allow new items to be created, and
serializer.save() # `.save()` will be called on updated or newly created instances. serializer.save() # `.save()` will be called on updated or newly created instances.
# `.delete()` will be called on any other items in the `queryset`. # `.delete()` will be called on any other items in the `queryset`.
Passing `allow_add_remove=True` ensures that any update operations will completely overwrite the existing queryset, rather than simply updating existing objects. Passing `allow_add_remove=True` ensures that any update operations will completely overwrite the existing queryset, rather than simply updating existing objects.
#### How identity is determined when performing bulk updates #### How identity is determined when performing bulk updates
...@@ -300,8 +302,7 @@ You can provide arbitrary additional context by passing a `context` argument whe ...@@ -300,8 +302,7 @@ You can provide arbitrary additional context by passing a `context` argument whe
The context dictionary can be used within any serializer field logic, such as a custom `.to_native()` method, by accessing the `self.context` attribute. The context dictionary can be used within any serializer field logic, such as a custom `.to_native()` method, by accessing the `self.context` attribute.
--- -
# ModelSerializer # ModelSerializer
Often you'll want serializer classes that map closely to model definitions. Often you'll want serializer classes that map closely to model definitions.
...@@ -344,6 +345,8 @@ The default `ModelSerializer` uses primary keys for relationships, but you can a ...@@ -344,6 +345,8 @@ The default `ModelSerializer` uses primary keys for relationships, but you can a
The `depth` option should be set to an integer value that indicates the depth of relationships that should be traversed before reverting to a flat representation. The `depth` option should be set to an integer value that indicates the depth of relationships that should be traversed before reverting to a flat representation.
If you want to customize the way the serialization is done (e.g. using `allow_add_remove`) you'll need to define the field yourself.
## Specifying which fields should be read-only ## Specifying which fields should be read-only
You may wish to specify multiple fields as read-only. Instead of adding each field explicitly with the `read_only=True` attribute, you may use the `read_only_fields` Meta option, like so: You may wish to specify multiple fields as read-only. Instead of adding each field explicitly with the `read_only=True` attribute, you may use the `read_only_fields` Meta option, like so:
......
...@@ -134,9 +134,9 @@ class RelatedField(WritableField): ...@@ -134,9 +134,9 @@ class RelatedField(WritableField):
value = obj value = obj
for component in source.split('.'): for component in source.split('.'):
value = get_component(value, component)
if value is None: if value is None:
break break
value = get_component(value, component)
except ObjectDoesNotExist: except ObjectDoesNotExist:
return None return None
...@@ -567,8 +567,13 @@ class HyperlinkedIdentityField(Field): ...@@ -567,8 +567,13 @@ class HyperlinkedIdentityField(Field):
May raise a `NoReverseMatch` if the `view_name` and `lookup_field` May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
attributes are not configured to correctly match the URL conf. attributes are not configured to correctly match the URL conf.
""" """
lookup_field = getattr(obj, self.lookup_field) lookup_field = getattr(obj, self.lookup_field, None)
kwargs = {self.lookup_field: lookup_field} kwargs = {self.lookup_field: lookup_field}
# Handle unsaved object case
if lookup_field is None:
return None
try: try:
return reverse(view_name, kwargs=kwargs, request=request, format=format) return reverse(view_name, kwargs=kwargs, request=request, format=format)
except NoReverseMatch: except NoReverseMatch:
......
...@@ -32,6 +32,9 @@ from rest_framework.relations import * ...@@ -32,6 +32,9 @@ from rest_framework.relations import *
from rest_framework.fields import * from rest_framework.fields import *
class RelationsList(list):
_deleted = []
class NestedValidationError(ValidationError): class NestedValidationError(ValidationError):
""" """
The default ValidationError behavior is to stringify each item in the list The default ValidationError behavior is to stringify each item in the list
...@@ -161,7 +164,6 @@ class BaseSerializer(WritableField): ...@@ -161,7 +164,6 @@ class BaseSerializer(WritableField):
self._data = None self._data = None
self._files = None self._files = None
self._errors = None self._errors = None
self._deleted = None
if many and instance is not None and not hasattr(instance, '__iter__'): if many and instance is not None and not hasattr(instance, '__iter__'):
raise ValueError('instance should be a queryset or other iterable with many=True') raise ValueError('instance should be a queryset or other iterable with many=True')
...@@ -336,9 +338,9 @@ class BaseSerializer(WritableField): ...@@ -336,9 +338,9 @@ class BaseSerializer(WritableField):
value = obj value = obj
for component in source.split('.'): for component in source.split('.'):
value = get_component(value, component)
if value is None: if value is None:
break return self.to_native(None)
value = get_component(value, component)
except ObjectDoesNotExist: except ObjectDoesNotExist:
return None return None
...@@ -378,6 +380,7 @@ class BaseSerializer(WritableField): ...@@ -378,6 +380,7 @@ class BaseSerializer(WritableField):
# Set the serializer object if it exists # Set the serializer object if it exists
obj = getattr(self.parent.object, field_name) if self.parent.object else None obj = getattr(self.parent.object, field_name) if self.parent.object else None
obj = obj.all() if is_simple_callable(getattr(obj, 'all', None)) else obj
if self.source == '*': if self.source == '*':
if value: if value:
...@@ -391,7 +394,8 @@ class BaseSerializer(WritableField): ...@@ -391,7 +394,8 @@ class BaseSerializer(WritableField):
'data': value, 'data': value,
'context': self.context, 'context': self.context,
'partial': self.partial, 'partial': self.partial,
'many': self.many 'many': self.many,
'allow_add_remove': self.allow_add_remove
} }
serializer = self.__class__(**kwargs) serializer = self.__class__(**kwargs)
...@@ -434,7 +438,7 @@ class BaseSerializer(WritableField): ...@@ -434,7 +438,7 @@ class BaseSerializer(WritableField):
DeprecationWarning, stacklevel=3) DeprecationWarning, stacklevel=3)
if many: if many:
ret = [] ret = RelationsList()
errors = [] errors = []
update = self.object is not None update = self.object is not None
...@@ -461,8 +465,8 @@ class BaseSerializer(WritableField): ...@@ -461,8 +465,8 @@ class BaseSerializer(WritableField):
ret.append(self.from_native(item, None)) ret.append(self.from_native(item, None))
errors.append(self._errors) errors.append(self._errors)
if update: if update and self.allow_add_remove:
self._deleted = identity_to_objects.values() ret._deleted = identity_to_objects.values()
self._errors = any(errors) and errors or [] self._errors = any(errors) and errors or []
else: else:
...@@ -514,12 +518,12 @@ class BaseSerializer(WritableField): ...@@ -514,12 +518,12 @@ class BaseSerializer(WritableField):
""" """
if isinstance(self.object, list): if isinstance(self.object, list):
[self.save_object(item, **kwargs) for item in self.object] [self.save_object(item, **kwargs) for item in self.object]
if self.object._deleted:
[self.delete_object(item) for item in self.object._deleted]
else: else:
self.save_object(self.object, **kwargs) self.save_object(self.object, **kwargs)
if self.allow_add_remove and self._deleted:
[self.delete_object(item) for item in self._deleted]
return self.object return self.object
def metadata(self): def metadata(self):
...@@ -795,9 +799,12 @@ class ModelSerializer(Serializer): ...@@ -795,9 +799,12 @@ class ModelSerializer(Serializer):
cls = self.opts.model cls = self.opts.model
opts = get_concrete_model(cls)._meta opts = get_concrete_model(cls)._meta
exclusions = [field.name for field in opts.fields + opts.many_to_many] exclusions = [field.name for field in opts.fields + opts.many_to_many]
for field_name, field in self.fields.items(): for field_name, field in self.fields.items():
field_name = field.source or field_name field_name = field.source or field_name
if field_name in exclusions and not field.read_only: if field_name in exclusions \
and not field.read_only \
and not isinstance(field, Serializer):
exclusions.remove(field_name) exclusions.remove(field_name)
return exclusions return exclusions
...@@ -823,6 +830,7 @@ class ModelSerializer(Serializer): ...@@ -823,6 +830,7 @@ class ModelSerializer(Serializer):
""" """
m2m_data = {} m2m_data = {}
related_data = {} related_data = {}
nested_forward_relations = {}
meta = self.opts.model._meta meta = self.opts.model._meta
# Reverse fk or one-to-one relations # Reverse fk or one-to-one relations
...@@ -842,6 +850,12 @@ class ModelSerializer(Serializer): ...@@ -842,6 +850,12 @@ class ModelSerializer(Serializer):
if field.name in attrs: if field.name in attrs:
m2m_data[field.name] = attrs.pop(field.name) m2m_data[field.name] = attrs.pop(field.name)
# Nested forward relations - These need to be marked so we can save
# them before saving the parent model instance.
for field_name in attrs.keys():
if isinstance(self.fields.get(field_name, None), Serializer):
nested_forward_relations[field_name] = attrs[field_name]
# Update an existing instance... # Update an existing instance...
if instance is not None: if instance is not None:
for key, val in attrs.items(): for key, val in attrs.items():
...@@ -857,6 +871,7 @@ class ModelSerializer(Serializer): ...@@ -857,6 +871,7 @@ class ModelSerializer(Serializer):
# at the point of save. # at the point of save.
instance._related_data = related_data instance._related_data = related_data
instance._m2m_data = m2m_data instance._m2m_data = m2m_data
instance._nested_forward_relations = nested_forward_relations
return instance return instance
...@@ -872,6 +887,14 @@ class ModelSerializer(Serializer): ...@@ -872,6 +887,14 @@ class ModelSerializer(Serializer):
""" """
Save the deserialized object and return it. Save the deserialized object and return it.
""" """
if getattr(obj, '_nested_forward_relations', None):
# Nested relationships need to be saved before we can save the
# parent instance.
for field_name, sub_object in obj._nested_forward_relations.items():
if sub_object:
self.save_object(sub_object)
setattr(obj, field_name, sub_object)
obj.save(**kwargs) obj.save(**kwargs)
if getattr(obj, '_m2m_data', None): if getattr(obj, '_m2m_data', None):
...@@ -881,7 +904,25 @@ class ModelSerializer(Serializer): ...@@ -881,7 +904,25 @@ class ModelSerializer(Serializer):
if getattr(obj, '_related_data', None): if getattr(obj, '_related_data', None):
for accessor_name, related in obj._related_data.items(): for accessor_name, related in obj._related_data.items():
setattr(obj, accessor_name, related) if isinstance(related, RelationsList):
# Nested reverse fk relationship
for related_item in related:
fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
setattr(related_item, fk_field, obj)
self.save_object(related_item)
# Delete any removed objects
if related._deleted:
[self.delete_object(item) for item in related._deleted]
elif isinstance(related, models.Model):
# Nested reverse one-one relationship
fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
setattr(related, fk_field, obj)
self.save_object(related)
else:
# Reverse FK or reverse one-one
setattr(obj, accessor_name, related)
del(obj._related_data) del(obj._related_data)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment