Pretty simple. I have a field parameter, dependent on another parameter.
I want to keep certain fields out of drop-down for this field parameter, e.g. OBJECTID. I don't want it, can't use it for this task, I don't want to see it.
How can I accomplish this?
I tried assigning a filter, but that's not supported on fields, so my current workaround is to use a multivalue string parameter and making the filter be all the fields whose names aren't ObjectID, etc.
Solved! Go to Solution.
Would something like this work?
p1.filter.list = [f.name for f in arcpy.ListFields(p0.value) if f.name not in ['OBJECTID', '<other undesireable fields>']]
That's almost exactly what I ended up doing.
I changed the Field Parameter to a string parameter (since I only needed the names and you can only filter field types), then
udFields.filters[0].list = [a.name for a in arcpy.ListFields(inExcel.value)
if a.name.lower() not in
["objectid", "globalid",
"guid", keyField.valueAsText.lower()]]
I should probably add OID in there, too, I guess.
Filters are supported for field parameters, but only for field types.
Field types can be found here: https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/field.htm
So for example, to only show numerical fields:
def getParameterInfo(self):
p0 = arcpy.Parameter(name="layer", displayName="Layer", datatype="GPFeatureLayer")
p1 = arcpy.Parameter(name="field", displayName="Field", datatype="Field"),
p1.parameterDependencies = ["layer"]
p1.filter.list = ["integer", "single", "double"]
return [p0, p1]
This sadly doesn't help with excluding specific field names, but maybe it's enough for your use case.
Would something like this work?
p1.filter.list = [f.name for f in arcpy.ListFields(p0.value) if f.name not in ['OBJECTID', '<other undesireable fields>']]
That's almost exactly what I ended up doing.
I changed the Field Parameter to a string parameter (since I only needed the names and you can only filter field types), then
udFields.filters[0].list = [a.name for a in arcpy.ListFields(inExcel.value)
if a.name.lower() not in
["objectid", "globalid",
"guid", keyField.valueAsText.lower()]]
I should probably add OID in there, too, I guess.