When originally setting this up I solved this problem ... somehow, now I can't recall how for the life of me.
After adding some fields to a file geodatabase feature class (TWP+"_RIGHTS"), I create and apply domains to three of the fields. The default is supposed to be "Choose", yet it just shows "<Null>". Any ideas how the problem can be corrected?
# create and set domains to fields
arcpy.env.workspace = projectfilepath + county+"_"+TWPfileGDB
RIGHTS = TWP + "_RIGHTS"
# Set local parameters
gdb = projectfilepath + county+"_"+TWPfileGDB
inFeatures = RIGHTS
default_value = 0
domName = "OpCo_Domain"
domain_description = "OpCo Choices"
inField = "OpCo"
# Process: Create the coded value domain
existingDomains = arcpy.da.ListDomains(gdb)
domain_names = [i.name for i in existingDomains]
if domName in domain_names:
print(domName + " already exists")
arcpy.management.AssignDomainToField(inFeatures, inField, domName)
arcpy.AssignDefaultToField_management(inFeatures, inField, default_value)
else:
print("creating domain " + domName)
arcpy.management.CreateDomain(gdb, domName, domain_description, "TEXT", "CODED")
domDict = {"0": "Choose", "1": "METC", "2": "ITCT"}
for code in domDict:
arcpy.management.AddCodedValueToDomain(gdb, domName, code, domDict[code])
# Process: Constrain the material value of distribution mains
arcpy.management.AssignDomainToField(inFeatures, inField, domName)
arcpy.AssignDefaultToField_management(inFeatures, inField, default_value)
domName_2 = "RightsType_Domain"
domain_description_2 = "RightsType Choices"
inField_2 = "RightsType"
existingDomains = arcpy.da.ListDomains(gdb)
domain_names = [i.name for i in existingDomains]
if domName_2 in domain_names:
print(domName_2 + " already exists")
arcpy.management.AssignDomainToField(inFeatures, inField_2, domName_2)
arcpy.AssignDefaultToField_management(inFeatures, inField_2, default_value)
else:
print("creating domain " + domName_2)
arcpy.management.CreateDomain(gdb, domName_2, domain_description_2, "TEXT", "CODED")
domDic_2 = {"0": "Choose", "1": "Easement", "2": "Fee", "3": "License"}
for code in domDic_2:
arcpy.management.AddCodedValueToDomain(gdb, domName_2, code, domDic_2[code])
# Process: Constrain the material value of distribution mains
arcpy.management.AssignDomainToField(inFeatures, inField_2, domName_2)
arcpy.AssignDefaultToField_management(inFeatures, inField_2, default_value)
domName_3 = "Type_of_Use_Domain"
domain_description_3 = "Type_of_Use Choices"
inField_3 = "Type_of_Use"
existingDomains = arcpy.da.ListDomains(gdb)
domain_names = [i.name for i in existingDomains]
if domName_3 in domain_names:
print(domName_3 + " already exists")
arcpy.management.AssignDomainToField(inFeatures, inField_3, domName_3)
arcpy.AssignDefaultToField_management(inFeatures, inField_3, default_value)
else:
print("creating domain " + domName_3)
arcpy.management.CreateDomain(gdb, domName_3, domain_description_3, "TEXT", "CODED")
domDict_3 = {"0": "Choose", "1": "Full T-Line", "2": "Substation", "3": "Railroad", "4": "Overhang Only",
"5": "Veg Management Only", "6": "Access", "7": "Other"}
for code in domDict_3:
arcpy.management.AddCodedValueToDomain(gdb, domName_3, code, domDict_3[code])
# Process: Constrain the material value of distribution mains
arcpy.management.AssignDomainToField(inFeatures, inField_3, domName_3)
arcpy.AssignDefaultToField_management(inFeatures, inField_3, default_value)
print("Completed"+" "+time.strftime("%x" +" "+ "(%a)" +" "+ "at"+" "+"%I:%M"))
Solved! Go to Solution.
Ah, I corrected my code to reference each domains' dictionary ! Thank you @DonMorrison1
field = "OpCo"
expression = 0
arcpy.management.CalculateField(RIGHTS, field, expression, "", "", "", "ENFORCE_DOMAINS")
field = "RightsType"
expression = 0
arcpy.management.CalculateField(RIGHTS, field, expression, "", "", "", "ENFORCE_DOMAINS")
field = "Type_of_Use"
expression = 0
arcpy.management.CalculateField(RIGHTS, field, expression, "", "", "", "ENFORCE_DOMAINS")
I believe that the defaults are not applied to existing records in your feature class, but the are applied only to new records being added. To update existing records you could run arcpy.management.CalculateField
@DonMorrison1 I'm not sure what you mean! I'm getting an error.
field = "Type_of_Use"
expression = "!Type_of_Use!.replace(' ', 'Choose')"
arcpy.management.CalculateField(RIGHTS, field, expression, "", "", "", "ENFORCE_DOMAINS")
Here's the error:
Traceback (most recent call last):
File "<expression>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'replace'
Ah, I corrected my code to reference each domains' dictionary ! Thank you @DonMorrison1
field = "OpCo"
expression = 0
arcpy.management.CalculateField(RIGHTS, field, expression, "", "", "", "ENFORCE_DOMAINS")
field = "RightsType"
expression = 0
arcpy.management.CalculateField(RIGHTS, field, expression, "", "", "", "ENFORCE_DOMAINS")
field = "Type_of_Use"
expression = 0
arcpy.management.CalculateField(RIGHTS, field, expression, "", "", "", "ENFORCE_DOMAINS")