Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- layers = self.iface.mapCanvas().layers()
- found = false
- for feature in layer.getFeatures():
- try:
- feature.attribute("fieldName") # Throws exception if it does not exist
- found = true
- except KeyError as e:
- # all features have hte same fields, so if it doesn't exists in the first feature, it won't exist in the others
- break
- finally:
- # all features have the same fields, so only need to check the first
- break
- field_name = "my_field"
- layer = iface.activeLayer()
- field_index = layer.fields().indexFromName(field_name)
- if field_index == -1:
- print("The field {} does not exist in layer {}!".format(field_name, layer.name()))
- else:
- print("The field {} exists in layer {}!".format(field_name, layer.name()))
Add Comment
Please, Sign In to add comment