Learning foreign keys

The application developer can turn any foreign key field into a learning foreign key.

learning foreign key

A foreign key that can "learn", i.e. add new items to its list of choices.

learning chooser

The chooser associated to a learning foreign key.

Usage

  • Define a chooser for the field by defining a FOO_choices() method on the model and decorating it with @dd.chooser.

  • Define an instance create_FOO_choice method on the model.

You can disable a learning foreign key by setting the lino.core.model.Model.disable_create_choice model attribute to True.

Examples

The city field of a postal address. When you specify the name of a city that does not yet exist, you simply leave the "invalid" city name in the combobox (Lino accepts it) and save the partner. Lino will then silently create a city of that name. Note that you must at least set the country, otherwise Lino refuses to automatically create a city. This behaviour is defined in the countries.CountryCity model mixin, which is inherited e.g. by contacts.Partner. or addresses.Address

Or the lino_xl.lib.contacts.Role.person field. You can see the new feature in every application with contacts. For example lino_book.projects.min1. In the detail of a company, you have the RolesByCompany slave table. In the Person column of that table you can type the name of a person that does not yet exist in the database. Lino will create it silently, and you can then click on the pointer to edit more information. Some examples in Automatically creating contact persons.

Implementation details

When a method is decorated with the chooser decorator, Lino creates a lino.utils.choosers.Chooser instance. The can_create_choice attribute of this instance will automatically be set to True when the field's model also has a method named create_FOO_choice (FOO being the field name).