Select to view content in your preferred language

Add features in a hosted feature layer using Python

329
4
Jump to solution
05-09-2025 02:31 AM
HamzaMerini
Occasional Contributor

Hello,

I created a Hosted Feature Layer in my portal, when I try to add features to this layer using python I get error 10500 Database error has occurred.

Here's the code:

from arcgis.gis import GIS
from arcgis.features import FeatureLayer

# Log in to GIS 
gis = GIS("https://myportal.com", "username", "password")

# URL of the hosted feature layer
feature_layer_url = "https://myportal.com/arcgis/rest/services/my_service/FeatureServer/0"

# Access the hosted feature layer
feature_layer = FeatureLayer(feature_layer_url)

# Example: Data to add 
user_data = [
    {"userid": "user_001"},
    {"userid": "user_002"},
    {"userid": "user_003"}
]

# List to store features to be added
features_to_add = []

# Convert the data into the required format for features (attributes)
for user in user_data:
    feature = {
        "attributes": {
            "userid": user["userid"]
        }
    }
    features_to_add.append(feature)

# Add the new features to the hosted feature layer
add_result = feature_layer.edit_features(adds=features_to_add)

# Check the result
if add_result['addResults'][0]['success']:
    print("Features added successfully.")
else:
    print(f"Error: {add_result['addResults'][0].get('error', 'Unknown error')}")

 

I'm trying to do it as I'm working with a normal layer and maybe this is the issue ?
The weird thing is that I was able to append data to the layer using a csv (directly from the portal) - but my aim is to use code

I already checked the settings:

HamzaMerini_0-1746783055395.png



Do you have any idea on how I can do it ? 
Thanks

0 Kudos
1 Solution

Accepted Solutions
HamzaMerini
Occasional Contributor

Here's what the prints look like 

HamzaMerini_0-1747039066907.png

Seems normal to me, I get the right data

View solution in original post

0 Kudos
4 Replies
ZiqingYu
Occasional Contributor

what do you see if you print the variable 'feature_layer'?

0 Kudos
DavidPike
MVP Notable Contributor

I'd recommend printing everything out and just try a simple hardcoded input to test.

0 Kudos
HamzaMerini
Occasional Contributor

Here's what the prints look like 

HamzaMerini_0-1747039066907.png

Seems normal to me, I get the right data

0 Kudos
HamzaMerini
Occasional Contributor

Apparently I had an issue with my db - issue is solved

0 Kudos