The AutoForm plugin for QGIS automatically sets the edit widget type for the fields of a selected layer based on their data types and foreign keys. This is in order to save the user time they may need to spend on manually editing these widgets.
In order for this to work correctly, the layer information must be stored in a PostgreSQL database. Furthermore, foreign keys must have a constraint rule for this to work. The plugin does NOT allow you to set relations. It merely checks for any and takes advantage of them accordingly.
##Motivation
When you have a database with a lot of tables, there should be a way for quickly entering data whithout having to configure the input form for each table first. In Switzerland this a common use case when you import Interlis data with ili2pg based on an official data model. ili2pg
automatically adds reference constraints based on the Interlis model, which is a prerequisite to detect relations in a generic way.
There is already a plugin with a very similar goal: DataDrivenInputMask written by Bernhard Ströbl. Maybe it was bad luck, but we had always troubles when using it. Once we eventually found out the PostgreSQL driver for the QtSql
library was missing and another time we failed to use the plugin because we had a socket based connection to PostgreSQL. In both cases we were stuck in the login dialog without a hint why the connection had failed. Another problem is in some cases that the DataDrivenInputMask plugin writes additional metadata into the original database.
So we decided to make our own proof of concept, hopefully avoiding these problems and having the Swiss “ili2pg” use case in mind. A major conceptual difference is, that AutoForm uses the built-in input form functionality of QGIS instead of creating custom dialogs like DataDrivenInputMask does.
##Running the plugin
After you have installed the AutoForm plugin (flagged “experimental”) via the QGIS Plugin repository you should see a new option in the Plugins
menu called AutoForm
. Clicking on it will give you the option Generate Form
. Running this with the layer selected will generate your form. If everything is correctly configured, this next step should be easy.
- Select a PostGIS layer from the Layers Panel
- Click on
Generate Form
And thats it! Now if you toggle editing you should see the changes which were made. In my case, this was the result:
If your layer had a foreign key reference to one or multiple other tables, then they too were added automatically to the Layers Panel and their values should be selectable in their according fields.
##Behind the Scenes
In order to perhaps help a bit with understanding how this Plugin works, I am adding this section for anyone who is curious. The Plugin follows this process:
- Check if a layer is selected
- If a layer is selected search for any key relationships to other tables
- If a foreign key relationship to another table was found the load that table into the project and set the corresponding fields to a Value Relation WidgetType
- Proceed normally and set the remaining Widgets where applicable
The script checks first if the field has already been changed, so as to prevent user made edits or the previously set Value Relations from being overwritten. Then it takes a look at the typeName() of the field in order to determine which widgettype to assign. For example if the field has the Date Type, it would use the calender widget. It does this for each field in the layer. Should the user wish to make additional changes (or correct any mistakes which might have been made), they can do so in the ‘Fields’ tab of the layer properties.
##Outlook
This plugin is in an early stage and serves as a proof of concept. There is much left to be done, especially in supporting embedded forms for related tables. We also don’t analyze additional metadata written by ili2pg
, which would allow to recognize whether related tables are only lookup tables without the need to embed them as entry forms.
The source code is available on Github. If you are interested in collaborating or maybe sponsoring improvements to this plugin, please contact us!