1

I have a point layer called "territories" with fields "species" and "red list status" (both text). The field "species" already contains values. "red list status" is NULL for all features.

Then I have a reference layer (excel file) called "reference" already imported into the project. The excel file also contains a field "species" and a value for "red list" (both text).

In QGIS, I created a project relation called "reference birds". The referenced layer is "reference", the referenced field is "species". The referencing layer is "territories" and the referencing field is "species".

Based on this project relation, I configured the attributes form of the layer "territories" in a way that, when I add a new feature with a value in field "species", the value for "red list status" is automatically filled in by QGIS. Works fine.

EDIT: The configuration for the field "red list status" in the layer "territories" is as following:

  • widget type: 'value relation'
  • Layer: "reference" (the excel file)
  • Key column: "species"
  • Value column: "red list status"
  • Filter expression: "species" = current_value('species')

BUT: I want to automatically fill in the value for "red list status" for features that already exist in the layer "territories". Ideally, this should be done based on the relation I created.

I tried to find a way to do this via the default value for field "red list" but I don't know how to involve the relation in the code (for example with CASE WHEN ELSE function).

How can I auto-update the values for field "red list" in my point layer based on my reference table so that the value for "species" is the condition for filling in the value for "red list"?

2
  • Please add the configuration for your "red list status" field to you question. Commented Aug 18 at 11:21
  • 1
    I added the field configuration in the main text Commented Aug 18 at 12:08

1 Answer 1

2
+50

To update existing features, you would need to use an expression to update them once off - using Field Calculator, something like this:

aggregate('reference',
          'concatenate',
          "red list status",
          "species" = attribute(@parent,'species'))

Since each species only matches up to one red list status, you could also use that expression in a default value (at the bottom of the widget settings) for "red list status". See screenshot below.

screenshot of default value configuration location

This negates the need to use the value relation widget for that field, as it will automatically fill in the red list value without you needing to select it.

When you configure the default value, you can also set it to 'Apply default value after update' which means that if you update any feature, it will re-apply this expression. You can decide whether you want to enable this, or disable this (which means it will only apply the value when you first digitise a feature).

If you enable 'Apply default value after update', another way to update previous features where "red list status" is empty is to trigger the expression to run by "updating" all the features. For example by using Field Calculator on the "species" field and using the expression "species" (i.e., reusing the same value, therefore not actually changing the data, but triggering an update anyway).

Note you cannot use the relation_aggregate() function in the referencing layer (i.e., territories layer in your example).

1
  • 1
    Thanks! Works perfect! The following is just a note for me to be able to comprehend the code line in the future when field names might change: aggregate('NAME OF THE REFERENCE LAYER', 'concatenate', "FIELD NAME FOR THE RED LIST STATUS IN THE REFERENCE LAYER", "FIELD NAME FOR THE SPECIES IN THE REFERENCE LAYER" = attribute(@parent,'FIELD NAME FOR THE SPECIES IN THE REFRENCING LAYER')) Commented Aug 28 at 7:06

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.