How to define a learning foreign key¶
When the model also defines a method
create_<fieldname>_choice, then the
chooser will become "learning": the ComboBox will be told to accept
also new values, and the server will handle these cases accordingly.
In the example application you can create new cities by simply typing them into the combobox.
class Person(dd.Model): ... def create_city_choice(self, text): """ Called when an unknown city name was given. Try to auto-create it. """ if self.country is not None: return rt.models.countries.Place.lookup_or_create( 'name', text, country=self.country) raise ValidationError( "Cannot auto-create city %r if country is empty", text)
Define a chooser for the field by defining a
FOO_choices()method on the model and decorating it with
Define an instance method
create_FOO_choiceon the model.
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 this on a
learning foreign key) 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
def create_city_choice(self, text): if self.country is not None: return rt.models.countries.Place.lookup_or_create( 'name', text, country=self.country) raise ValidationError( "Cannot auto-create city %r if country is empty", text)
lino_xl.lib.contacts.Role.person field. You can see the feature
in every application with contacts. For example
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
def create_person_choice(self, text): return rt.models.contacts.Person.create_from_choice(text)
See also doctest snippets in Automatically creating contact persons.
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
(FOO being the field name).