Defines the Model class.

See About database models, Customize delete behaviour, Disabling elements of the user interface, Hiding individual fields, Customizing how data is formatted

(This module's source code is available here.)


pre_delete_handler(sender[, instance])

Before actually deleting an object, we override Django's behaviour concerning related objects via a GFK field.


Model(*args, **kwargs)

Lino's extension of the plain Django Model class.

class lino.core.model.Model(*args, **kwargs)

Bases: django.db.models.base.Model, lino.core.fields.TableRow

Lino's extension of the plain Django Model class.


This is defined by Django.

active_fields = frozenset({})

Deprecated. If specified, this is the default value for active_fields of every Table on this model.

workflow_state_field = None

If this is set on a Model, then it will be used as default value for workflow_state_field of all tables based on this Model.

workflow_owner_field = None

If this is set on a Model, then it will be used as default value for lino.core.table.Table.workflow_owner_field on all tables based on this Model.

change_watcher_spec = None

Internally used by watch_changes()

classmethod collect_virtual_fields()

Declare every virtual field defined on this model to Django.

We use Django's undocumented add_field() method.

Make a copy if the field is inherited, in order to avoid side effects like #2592.

Raise an exception if the model defines both a database field and a virtual field of same name.


Double-check to avoid "murder bug" (20150623).

classmethod define_action(**kw)

Adds one or several actions or other class attributes to this model.

Attributes must be specified using keyword arguments, the specified keys must not yet exist on the model.

Used e.g. in to add the UpdateReminders action to :class: lino.modlib.users.models.User.

Or in lino_xl.lib.invoicing.models for defining a custom chooser.

classmethod add_param_filter(qs, lookup_prefix='', **kwargs)

Add filters to queryset using table parameter fields.

This is called for every simple parameter.

Usage examples: DeploymentsByTicket, lino_book.projects.min3.lib.contacts.

classmethod lookup_or_create(lookup_field, value, **known_values)

Look up whether there is a model instance having lookup_field with value value (and optionally other known_values matching exactly).

If it doesn't exist, create it and emit an auto_create signal.

classmethod quick_search_filter(search_text, prefix='')

Return the filter expression to apply when a quick search text is specified.


Save this instance and fire related behaviour.

get_row_permission(ar, state, ba)

Returns True or False whether this database object gives permission to the ActionRequest ar to run the specified action.


Return or yield a list of (type,partner) tuples to be used as recipents when creating an outbox.Mail from this object.


Return or yield a list of Partners to be used as recipents when creating a posting.Post from this object.

before_state_change(ar, old, new)

Called by set_workflow_state() before a state change.

after_state_change(ar, old, new)

Called by set_workflow_state() after a state change.

set_workflow_state(ar, state_field, target_state)

Called by workflow actions (ChangeStateAction) to perform the actual state change.

after_send_mail(mail, ar, kw)

Called when an outbox email controlled by self has been sent (i.e. when the lino_xl.lib.outbox.models.SendMail action has successfully completed).


Used when implementing Polymorphism.

classmethod get_request_queryset(ar, **filter)

Return the base queryset for tables on this object.

The optional filter keyword arguments, if present, are applied as additional filter. This is used only in UNION tables on abstract model mixins where filtering cannot be done after the join.

classmethod get_user_queryset(user, **filter)

Get the base queryset, used for user level row filtering in

classmethod resolve_states(states)

Convert the given string states into a set of state objects.

The states specifier must be either a set containing state objects or a string containing a space-separated list of valid state names. If it is a string, convert it to the set.

classmethod get_actions_hotkeys()

Return or yield a list of hotkeys to be linked to named actions.

[{'key': key, 'ctrl': Bool, 'shift': Bool, 'ba': action_name}]

classmethod get_layout_aliases()

Yield a series of (ALIAS, repl) tuples that cause a name ALIAS in a layout based on this model to be replaced by its replacement repl.

classmethod add_picker(fldname)

Add a picker for the named choicelist field.

A picker is a virtual field that shows all the available choices in a way that you can click on them to change the underlying choicelist field's value. Functionally similar to a radio button, but causes immediate submit instead of waiting until the form gets submitted.

classmethod django2lino(model)

Inject Lino model methods into a pure Django model that does not inherit from lino.core.model.Model.

classmethod get_subclasses_graph()

Returns an internationalized graphviz directive representing the polymorphic forms of this model.

Usage example:

.. django2rst::

    with dd.translation.override('de'):
lino.core.model.pre_delete_handler(sender, instance=None, **kw)

Before actually deleting an object, we override Django's behaviour concerning related objects via a GFK field.

In Lino you can configure the cascading behaviour using allow_cascaded_delete.

See also GenericForeignKey fields.

It seems that Django deletes generic related objects only if the object being deleted has a GenericRelation field (according to Why won't my GenericForeignKey cascade when deleting?). OTOH this statement seems to be wrong: it happens also in my projects which do not use any GenericRelation. As test_broken_gfk shows.

TODO: instead of calling disable_delete here again (it has been called earlier by the delete action before asking for user confirmation), Lino might change the on_delete attribute of all ForeignKey fields which are not in allow_cascaded_delete from CASCADE to PROTECTED at startup.