I am following the instructions here (https://support.esri.com/en/technical-article/000020083) to create a hosted feature layer view and set the definition. I am able to change items such as name, viewDefinitionQuery, and drawingInfo, but I have been unsuccessful in setting the list of fields to be available (which I think you do by updating the "fields" array in the definition? not entirely sure). After supplying a dictionary to the update_definition method I get a JSON response of 'success' although the fields have not been changed.
# after getting the original hosted feature layer, I make a feature layer collection
source_flc = FeatureLayerCollection.fromitem(source_item)
# make a view from the feature layer collection
new_view = source_flc.manager.create_view(name="testview")
# search for the new view (per instructions at url above but it also looks like you can
# just pick up new_view? Is there a difference?
view_search = gis.content.search("testview")[0]
# and why do the directions have you make another feature layer collection?
# I have tried it this way as well as setting service_layer to view_search.layers[0]
# and new_view.layers[0]. Both work.
view_flc = FeatureLayerCollection.fromitem(view_search)
service_layer = view_flc.layers[0]
# in this case, I copied the fields I wanted from the JSON view of the
# hosted feature layer view properties into one big string, but I
# have also tried a python list and a JSON object
fields = '''[
{
"name" : "OBJECTID",
"type" : "esriFieldTypeOID",
"alias" : "OBJECTID",
"sqlType" : "sqlTypeOther",
"nullable" : false,
"editable" : false,
"domain" : null,
"defaultValue" : null
},
{
"name" : "AGDB_ID",
"type" : "esriFieldTypeInteger",
"alias" : "AGDB_ID",
"sqlType" : "sqlTypeOther",
"nullable" : true,
"editable" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "LAB_ID",
"type" : "esriFieldTypeString",
"alias" : "LAB_ID",
"sqlType" : "sqlTypeOther",
"length" : 255,
"nullable" : true,
"editable" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "PREV_LAB_ID_1",
"type" : "esriFieldTypeString",
"alias" : "PREV_LAB_ID_1",
"sqlType" : "sqlTypeOther",
"length" : 255,
"nullable" : true,
"editable" : true,
"domain" : null,
"defaultValue" : null
}]'''
# create the update dictionary, adding the keys "name" and "fields"
update_dict = {"name" : "foobar", "fields": fields}
service_layer.manager.update_definition(update_dict)
# have to search for the layer properties again after setting them
# to see what has changed
gis.content.search("testview")[0].layers[0].properties
At this point I can see that name has been changed, but all of the original fields are included in the definition