I've set-up a project with an editable layer (PostGis) with linear geographic elements in qgis and Lizmap.
I would like some fields to be automaticaly filled when an user create a new object. List of fields I would like to use:
- Primary key
- The name of the user who created the entity
- The date of creation / modification
I've searched many threads and places, but can't find the exact how to do.
Environment
Lizmap version: 3.4.8
QGIS Server FULL version: 3.4.4
QGIS Desktop FULL version: 3.16.5
OS (Windows, Linux, MacOS, Android...): Windows
Browser (Firefox, Chrome...): Firefox
Edit:
This is how looks my table in the database manager
Here is the table summary from QGIS database manager
This is what I've set-up in the form manager of the layer within QGIS. It works when creating new feature in QGIS but not in Lizmap enter image description here
-
What do you mean as user default value ? a standard value for every user OR the lizmap user name of last person who edited it ?Corentin– Corentin2022年01月13日 16:36:44 +00:00Commented Jan 13, 2022 at 16:36
-
By the way, if you are really using QGIS Server 3.4 with QGIS Desktop 3.16, we should expect issues ! QGIS Server and desktop must be the same version. QGIS Server can't read XML files generated in the future with QGIS Desktop 3.16 (2 years after)etrimaille– etrimaille2022年01月13日 18:09:33 +00:00Commented Jan 13, 2022 at 18:09
-
@CorentinLemaitre Yes exactly. I would like the lizmap user name of last person who edited itArthur– Arthur2022年01月14日 08:40:53 +00:00Commented Jan 14, 2022 at 8:40
-
@etrimaille I know but the IT service struggle to update it. They said even with LTR source enable they only get QGis version 3.4.15-Madeira. Don't know why as I don't have direct background access myself.Arthur– Arthur2022年01月14日 08:40:56 +00:00Commented Jan 14, 2022 at 8:40
-
Which OS and version is your server ? 3.4.15 is very old. In the meantime, you got two LTR 3.10 and 3.16. And since today, 3.22.3 is the new LTR.etrimaille– etrimaille2022年01月14日 16:15:46 +00:00Commented Jan 14, 2022 at 16:15
3 Answers 3
Go to layer properties >>> attribut form you can create primary key with uuid function on your table and for date you use now() fonction. dont forget to set as default value. for the name of the user you can create a list of choices.
-
Hi, thank you for your comment. The table is a posgres table, as this is mandatory for an editable layer within lizmap. Therefore I already have an id field. But when creating a feature I got a pronpting pop-up asking me to manually fill this field. I would like something automaticArthur– Arthur2022年01月14日 14:39:00 +00:00Commented Jan 14, 2022 at 14:39
-
If your field is autoincrement, then you can hide from the form. You use the "drag and drop layout" and remove it from the form. Is-it OK ? It won't be displayed in QGIS Desktop as well in the form.etrimaille– etrimaille2022年01月14日 16:13:23 +00:00Commented Jan 14, 2022 at 16:13
-
@etrimaille I manage to use the "drag and drop layout" to remove from the form the unique id and user field. But when saving a new drawing got this error message: La saisie de "identifiant unique" est obligatoire La saisie de "user" est obligatoire Meaning it doesn't autofillArthur– Arthur2022年01月17日 09:26:56 +00:00Commented Jan 17, 2022 at 9:26
-
Which Lizmap Web Client version are you using ? Is-there a default value ? Can you add these answers in your question, what you tried etc.etrimaille– etrimaille2022年01月17日 09:33:06 +00:00Commented Jan 17, 2022 at 9:33
-
I use Lizmap Web Client 3.4.8. Default value for id flied is set as: "uuid()" as @LM10 suggested from the attribute form. I will add some info in my first post as you suggestArthur– Arthur2022年01月17日 10:17:27 +00:00Commented Jan 17, 2022 at 10:17
For the current user, this is done :
- if your layer is filtered by login. Source 1
- Using the variable
@lizmap_user
with LM10's user (about setting up the form). But this will work only for new features. The "apply on update" checkbox is not supported for now. Source 2.
Sources :
As you have a layer that could be edited, it is a PostGIS layer. To get a primary with PostGIS you can not use the attribute form control in QGIS, you must define it within the DB manager.
Step 1 : create the DB table with primary key
The easiest way is to use a layer you have in your computer and import it in PostGIS. You have to prepare your layer and change attribute name to get no space in it (you could get some strange result if not). The import vector layer toolbox will let you choose the field name used for primary key or to create one (if you choose a field name that doesn't exist in your layer).
This will create a layer in PostGIS database that has a primary key. If you use the DB manager and reconnect to your DB and open the info of your table you should see two things :
- in the fields, one have Null =
N
and Default field. - You should see a constraints type primary key
Step 2 option A : add default value for time
You can define the default value in DB or in QGIS form proprieties (answered before). To define it in the DB you have to use the SQL querry windows and add a default value expression (warning, SQL expression is not the same as QGIS expression).
Here is an example for time attribute (the field have to be created before) :
ALTER TABLE "schema_name"."table_name" ALTER COLUMN "time" SET DEFAULT now();
As well, you can define a default value for a field named user.
Step 2 option B : Use logging of every change done by users
In QGIS database manager, there is the tool "Change logging" to log every change made in the database and keep history of modification.
This tool will add username, start_date and end_date automatically. But to use it, you have to empty your database then use the tool with default option.
This will modify your current table (add new fields and triggers and rules) and create a new table named TABLENAME_current.
Then add the layer to your project, activate a filter "time_end" is NULL
and copy all your features in this layer. Each modification will create a new feature and add a time end to the old feature. In the form you can hide : id_hist, id, time_end, time_start, user_role
-
Hi, thanks for your time. You are correct, I have a postgres table with a unique ID field. The problem is i can't get it to be automaticaly field. I always get a promp within lizmap to fill it manually. I tried ALTER TABLE "schema_name"."table_name" ALTER COLUMN "id" SET DEFAULT uuid_generate_v1(); but got an error For the date field, your expression worked well within qgis db. Now in Lizmap is set the correct date by defaut. But I would like the user not to be able to change it. Is this also possible?Arthur– Arthur2022年01月14日 15:25:24 +00:00Commented Jan 14, 2022 at 15:25
-
BUt do you want the field to "disabled" or "hidden" for the form ?etrimaille– etrimaille2022年01月14日 16:36:52 +00:00Commented Jan 14, 2022 at 16:36
-
If you did create the table like presented up, then the field is automatically updated and the edition should work well both web and on QGIS. Then as @etrimaille said there is an option to hide it in the form to get no changes from users.Corentin– Corentin2022年01月15日 10:11:28 +00:00Commented Jan 15, 2022 at 10:11
-
I tried 2 times your solution, but never got a default setting for the id field. Therefore I continue to struggle to save a new entity from Lizmap at it ask me to manualy enter the id. I tried multiple solutions mentioned else where as using attribute form, but it works in QGIS Desktop but not in Lizmap.Arthur– Arthur2022年01月18日 13:22:49 +00:00Commented Jan 18, 2022 at 13:22
-
You should delete your table and import it again with primary key activated as step 1 explain. It is hard to define a primarey key after creating your table, you need to create a sequence, then a default value based on nextval of this sequence and automatically update this sequence based on id...Corentin– Corentin2022年01月19日 17:49:05 +00:00Commented Jan 19, 2022 at 17:49