I try to generate a "UUID" (edit widget) for a vector shapefile in QGIS 2 but the field value is always NULL. Does the field requires a specific datatype? I used integer. In the manual I found the info "what uuid-generator is" but not how to use it.

1 Answer 1


I struggled for a bit to figure this out, too...

First off, it is important to understand what is a UUID (or GUID)?

GUIDs are usually stored as 128-bit values, and are commonly displayed as 32 hexadecimal digits with groups separated by hyphens, such as {21EC2020-3AEA-1069-A2DD-08002B30309D}. GUIDs generated from random numbers sometimes contain 6 fixed bits saying they are random and 122 random bits;

As far as QGIS is concerned, this would be a 36 character string that will look something like 3a2a47bf-ccaf-4aa9-8100-cb3abceec3e7, so you would not be able to use a INT field type.

Next, using the UUID Generator widget is a "manual" process that edits a single record at a time. To do so:

  1. Set your Properties-> Fields-> Edit Widget is set to "UUID generator".
  2. Open your attribute table
  3. Toggle on edit mode
  4. Double-click in the empty field (set in step 1) to generate a new UUID for that record.

If what you are really after is a way to automatically generate UUID's for all features, @NathanW modified this 2011 code to work with QGIS 2:

import uuid
fieldname = 'UUID' # set the name of your field to update

layer = qgis.utils.iface.activeLayer() # get the active layer from the TOC
layer.startEditing() # start editing

iter = layer.getFeatures() # get all features from the layer
for feat in iter: # iterate over features
    feat[fieldname] = str(uuid.uuid4()) #fill field with GUID
    layer.updateFeature(feat) #update the attribute value

layer.commitChanges() # save edits to the layer

Then, do the following:

  1. Highlight the layer to update (in the TOC)
  2. Open up the Python Console,
  3. Press the "Show Editor" button,
  4. Paste this code into the Python Console,
  5. Change the FIELDNAME variable to match the name of your field in your layer,
  6. Press the "Run script" button.
  • Thanks for your answer! After your introduction to UUID (or GUID) I think it is not what I’m looking for… Anyway, I tried both solutions and it works. To run the script I had to add the following snippet from your 2011-script to get the active layer! layer=qgis.utils.iface.activeLayer()
    – parallax
    Oct 8, 2013 at 19:20
  • Thanks for the comment. I've updated the code snippet. If this answered your question, please click the "checkmark" next to the answer to mark it as "accepted". Oct 8, 2013 at 19:27
  • You can reduce to the code to this gist.github.com/NathanW2/6901558 (A little more overhead but cleaner)
    – Nathan W
    Oct 9, 2013 at 13:46
  • @NathanW, thanks for the code update. I've updated the answer above. It would be great if you could pass along this code to the QGIS team and get a UUID generator added to the default "Field Calculator" functions. Maybe under the "String" function List? That way you could update all records through the field calculator with a function like "MyField" = uuid(). Oct 9, 2013 at 15:12
  • You mean like this ;) github.com/qgis/QGIS/commit/…
    – Nathan W
    Oct 10, 2013 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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