So I have created a custom form for my attribute table in QGIS using the drag and drop designer option. I also have an external python init function that I imported. Now Let's say I create a new Feature. After I create the feature the custom form pops up and my python file checks if a specific attribute of the feature is not defined(or is NULL). If it is NULL or not defined I set the attribute. My code for this part looks like this:

if feature["name"] is None:
   feature.setAttribute("name", "Max")

Until now everything works. My problem is now that if I open the attribute table in qgis I always get an KeyError, but the attribute was set successfully. The KeyError look like this KeyError: 'name'. It might be hard to understand but I can't describe it better. Does anyone know what I am doing wrong?


I solved my problem with these two line:

fields = layer.fields()

What I did was give the feature all fields that exist in the layer, because sometimes some features don't get all of them. That's what I think was the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.