nodes: 0, module; 1, function_definition; 2, function_name:raise_errors_on_nested_writes; 3, parameters; 4, block; 5, identifier:method_name; 6, identifier:serializer; 7, identifier:validated_data; 8, expression_statement; 9, comment:# Ensure we don't have a writable nested field. For example:; 10, comment:#; 11, comment:# class UserSerializer(ModelSerializer):; 12, comment:#     ...; 13, comment:#     profile = ProfileSerializer(); 14, assert_statement; 15, comment:# Ensure we don't have a writable dotted-source field. For example:; 16, comment:#; 17, comment:# class UserSerializer(ModelSerializer):; 18, comment:#     ...; 19, comment:#     address = serializer.CharField('profile.address'); 20, assert_statement; 21, comment:"""
      Give explicit errors when users attempt to pass writable nested data.
  
      If we don't do this explicitly they'd get a less helpful error when
      calling `.save()` on the serializer.
  
      We don't *automatically* support these sorts of nested writes because
      there are too many ambiguities to define a default behavior.
  
      Eg. Suppose we have a `UserSerializer` with a nested profile. How should
      we handle the case of an update, where the `profile` relationship does
      not exist? Any of the following might be valid:
  
      * Raise an application error.
      * Silently ignore the nested part of the update.
      * Automatically create a profile instance.
      """; 22, not_operator; 23, parenthesized_expression; 24, not_operator; 25, parenthesized_expression; 26, call; 27, call; 28, call; 29, call; 30, identifier:any; 31, generator_expression; 32, attribute; 33, argument_list; 34, identifier:any; 35, generator_expression; 36, attribute; 37, argument_list; 38, boolean_operator; 39, for_in_clause; 40, concatenated_string; 41, identifier:format; 42, keyword_argument; 43, keyword_argument; 44, keyword_argument; 45, boolean_operator; 46, for_in_clause; 47, concatenated_string; 48, identifier:format; 49, keyword_argument; 50, keyword_argument; 51, keyword_argument; 52, boolean_operator; 53, call; 54, pattern_list; 55, call; 56, string; 57, string; 58, string; 59, string; 60, identifier:method_name; 61, identifier:method_name; 62, identifier:module; 63, attribute; 64, identifier:class_name; 65, attribute; 66, boolean_operator; 67, call; 68, pattern_list; 69, call; 70, string; 71, string; 72, string; 73, string; 74, identifier:method_name; 75, identifier:method_name; 76, identifier:module; 77, attribute; 78, identifier:class_name; 79, attribute; 80, call; 81, parenthesized_expression; 82, identifier:isinstance; 83, argument_list; 84, identifier:key; 85, identifier:field; 86, attribute; 87, argument_list; 88, string_content:The `.{method_name}()` method does not support writable nested; 89, string_content; 90, string_content:serializer `{module}.{class_name}`, or set `read_only=True` on; 91, string_content:nested serializer fields.; 92, attribute; 93, identifier:__module__; 94, attribute; 95, identifier:__name__; 96, comparison_operator:'.' in field.source; 97, parenthesized_expression; 98, identifier:isinstance; 99, argument_list; 100, identifier:key; 101, identifier:field; 102, attribute; 103, argument_list; 104, string_content:The `.{method_name}()` method does not support writable dotted-source; 105, string_content; 106, string_content:serializer `{module}.{class_name}`, or set `read_only=True` on; 107, string_content:dotted-source serializer fields.; 108, attribute; 109, identifier:__module__; 110, attribute; 111, identifier:__name__; 112, identifier:isinstance; 113, argument_list; 114, comparison_operator:key in validated_data; 115, subscript; 116, tuple; 117, attribute; 118, identifier:items; 119, escape_sequence:\n; 120, identifier:serializer; 121, identifier:__class__; 122, identifier:serializer; 123, identifier:__class__; 124, string; 125, attribute; 126, comparison_operator:key in validated_data; 127, subscript; 128, tuple; 129, attribute; 130, identifier:items; 131, escape_sequence:\n; 132, identifier:serializer; 133, identifier:__class__; 134, identifier:serializer; 135, identifier:__class__; 136, identifier:field; 137, identifier:BaseSerializer; 138, identifier:key; 139, identifier:validated_data; 140, identifier:validated_data; 141, identifier:key; 142, identifier:list; 143, identifier:dict; 144, identifier:serializer; 145, identifier:fields; 146, string_content:.; 147, identifier:field; 148, identifier:source; 149, identifier:key; 150, identifier:validated_data; 151, identifier:validated_data; 152, identifier:key; 153, identifier:list; 154, identifier:dict; 155, identifier:serializer; 156, identifier:fields
code: def raise_errors_on_nested_writes(method_name, serializer, validated_data):
      """
      Give explicit errors when users attempt to pass writable nested data.
  
      If we don't do this explicitly they'd get a less helpful error when
      calling `.save()` on the serializer.
  
      We don't *automatically* support these sorts of nested writes because
      there are too many ambiguities to define a default behavior.
  
      Eg. Suppose we have a `UserSerializer` with a nested profile. How should
      we handle the case of an update, where the `profile` relationship does
      not exist? Any of the following might be valid:
  
      * Raise an application error.
      * Silently ignore the nested part of the update.
      * Automatically create a profile instance.
      """
  
      # Ensure we don't have a writable nested field. For example:
      #
      # class UserSerializer(ModelSerializer):
      #     ...
      #     profile = ProfileSerializer()
      assert not any(
          isinstance(field, BaseSerializer) and
          (key in validated_data) and
          isinstance(validated_data[key], (list, dict))
          for key, field in serializer.fields.items()
      ), (
          'The `.{method_name}()` method does not support writable nested'
          'fields by default.\nWrite an explicit `.{method_name}()` method for '
          'serializer `{module}.{class_name}`, or set `read_only=True` on '
          'nested serializer fields.'.format(
              method_name=method_name,
              module=serializer.__class__.__module__,
              class_name=serializer.__class__.__name__
          )
      )
  
      # Ensure we don't have a writable dotted-source field. For example:
      #
      # class UserSerializer(ModelSerializer):
      #     ...
      #     address = serializer.CharField('profile.address')
      assert not any(
          '.' in field.source and
          (key in validated_data) and
          isinstance(validated_data[key], (list, dict))
          for key, field in serializer.fields.items()
      ), (
          'The `.{method_name}()` method does not support writable dotted-source '
          'fields by default.\nWrite an explicit `.{method_name}()` method for '
          'serializer `{module}.{class_name}`, or set `read_only=True` on '
          'dotted-source serializer fields.'.format(
              method_name=method_name,
              module=serializer.__class__.__module__,
              class_name=serializer.__class__.__name__
          )
      )