Problem with simple definition query expression

1895
10
04-28-2017 02:41 AM
KONPETROV
Occasional Contributor III

I am trying to assign a value to two shapefiles in the first data frame of my .mxd with definition query, but it seems that cannot be done.

Any suggestion? Thanks

Here is the code:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd):  
    if lyr.name == "waste":  
        lyr.definitionQuery == "Code = 'IT1300021'"
0 Kudos
10 Replies
DanPatterson_Retired
MVP Emeritus

what is the error or what is the result?

lyr.definitionQuery == .... this is an equality check not an assignment, which is what I think you want

KONPETROV
Occasional Contributor III

good note, but is not working either with that. I get an error Runtime error SyntaxError: can't assign to comparison (<string>, line 6)

0 Kudos
DanPatterson_Retired
MVP Emeritus

So no error? just nothing is happening?

It might be time to add

if lyr.supports("DEFINITIONQUERY"):

to the test to ensure your layers support a query and a couple of print statement to monitor progress, otherwise a malformed query should have at least thrown an error.

0 Kudos
KONPETROV
Occasional Contributor III

I wrote that but...

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
aa = "'IT1300021'"
for lyr in arcpy.mapping.ListLayers(mxd):
   if lyr.supports("DEFINITIONQUERY"):
       if lyr.name == "waste":  
           lyr.definitionQuery == "Code = 'IT1300021'"
0 Kudos
KONPETROV
Occasional Contributor III

I ve also tried this, but didn't work also

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
aa = "'IT1300021'"
for lyr in arcpy.mapping.ListLayers(mxd):  
    if lyr.name == "waste":  
        lyr.definitionQuery == "'Code = {}'".format(aa)
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In both of your follow-up code snippets, you are still using an equality check (==) instead of an assignment (=).  Even if all of the lines up to the final line worked, the final line of code isn't' going to update anything, it is only going to return a True or False that has no impact on anything.

0 Kudos
DanPatterson_Retired
MVP Emeritus

well lacking print statements, it isn't possible to see the code even gets into the sections where the definitions are being set.  I can only refer you to the help topic where it states that the source and type of the layer is important, case sensitivity applies to one type and not another and that changes are not automatic but an update may need to be made.  Hence, my recommendation about print statements to see what is going on....it is probably something obvious

http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/updatingandfixingdatasources.htm

http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/layer-class.htm

0 Kudos
MitchHolley1
MVP Regular Contributor

Like Dan said, you should use = instead of ==. 

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd):  
    if lyr.name == "waste":  
        lyr.definitionQuery = "Code = 'IT1300021'"

arcpy.RefreshActiveView()
0 Kudos
MitchHolley1
MVP Regular Contributor

What's the status of this issue?

0 Kudos