Set default value for a field in ArcMap using python

10001
18
05-06-2015 09:30 AM
StacyMcNeil
New Contributor II


I have been trying to figure out how I can set a default value to a field in ArcMap.  I see there is a tool called Assign Default to field but that is applied to the entire dataset.  I would like the default to apply to the current ArcMap session only.  In the Create Features window you can right click a layer and set a default there to get the behavior I would like but am trying to accomplish this for multiple layers in the TOC using python.

This is what I have been trying, starting with one layer first, but get an error "The attribute 'defaultValue' is not supported on this instance of Field"  I can print the defaultValue however.

feature_class = "Roads"

fields = arcpy.ListFields(feature_class)

for field in fields:

     if field.name == "Project_Name":

          field.defaultValue = "Test_Project"

         

Forgive me if I am making an obvious mistake.  I am still a beginner.  Thanks for any help!

0 Kudos
18 Replies
SepheFox
Frequent Contributor

Hi Stacy, it looks like you're on the right track. Try using Assign Default Values, instead of field.defaultValue: Assign Default Values.

0 Kudos
ChristianWells
Esri Regular Contributor

Hi Stacy,

Try out the Assign Default To Field (Data Management) tool.

feature_class = "Roads"
fields = arcpy.ListFields(feature_class)

for field in fields:
     if field.name == "Project_Name":
          arcpy.AssignDefaultToField_management(feature_class, "Project_Name", "Test_Project")
0 Kudos
StacyMcNeil
New Contributor II

Thanks Christian and Sephe.  I have tried th Assign Default to Field tool and the default value then gets applied to the entire feature class so anyone editing will have the same default value.  I need the default to change from map to map.  Any ideas on that?

0 Kudos
BlakeTerhune
MVP Regular Contributor

There's no easy, built-in way to do this. Have you considered using Subtypes? You can have different default field values based on whatever is chosen for the subtype.

0 Kudos
curtvprice
MVP Esteemed Contributor

I need the default to change from map to map.  Any ideas on that?

You could package the mxd with a toolbox, with a model or script tool that runs that default value tool with a parameter for the value you set up or have them pick from a list. You can even a button on a toolbar in the mxd interface - and instruct the user to run it before editing.

Outside of that you would have to build an AddIn which may be more than you want to get into right now. That also would be a different learning curve on the user end (loading the addin, adding it to the interface) so I would prefer the first method anyway.

0 Kudos
SepheFox
Frequent Contributor

for field in fields: 

     if field.name == "Project_Name":

          mxd = arcpy.mapping.MapDocument("CURRENT")

          default = mxd.name()

          arcpy.AssignDefaultToField_management(feature_class, "Project_Name", default) 

0 Kudos
curtvprice
MVP Esteemed Contributor

Here's a function implementation. BTW mxd does not a name property, just filePath.

import os
import arcpy
def AssignDefault(tbl, fld):
    try:
        fname = arcpy.ListFields(tbl, fld)[0].name
        mxd = arcpy.mapping.MapDocument("CURRENT")
        default = os.path.splitext(arcpy.Describe(mxd.filePath).name)[0]
        arcpy.AssignDefaultToField_management(tbl, fname, default) 
    except:
        print("Could not assign default")
0 Kudos
StacyMcNeil
New Contributor II
import os  
import arcpy
tbl = "Roads"
fld = "Project_Name"
def AssignDefault(tbl, fld):  
    try:  
        fname = arcpy.ListFields(tbl, fld)[0].name  
        mxd = arcpy.mapping.MapDocument("CURRENT")  
        default = os.path.splitext(arcpy.Describe(mxd.filePath).name)[0]  
        arcpy.AssignDefaultToField_management(tbl, fname, default)
        print default
    except:  
        print("Could not assign default")

Thanks Curtis.  I will be creating a tool for the user to enter their Project Name but am keeping this simple for now since I don't even know what I am trying to do is possible.  I ran your code frmo ArcMap but nothing happened.  I added a print statement and that was not displayed either.  Any ideas?

0 Kudos
DanPatterson_Retired
MVP Emeritus

you never call the function ie

AssignDefault(tbl, fld)

is needed as a line, and the function itself should be put at the top of your script after the import statements

0 Kudos