About virtual fields

database field

A regular Django model field. CharField, ForeignKey, IntegerField, TextField, BooleanField, etc.

virtual field

A data element whose value isn't stored in the database but computed on the fly each time we want to see it. For example the age of a person is usually computed from the birth_data. It wouldn't make sense to store this number in the database.


Define a virtual field on a model:

def bus_needed(self, ar):
    return self.get_places_sum(
        state=EnrolmentStates.requested, needs_bus=True)

(Taken from lino_avanti.lib.avanti.Client)

Define a virtual field on a table:

def question(self, obj, ar):
    return obj

(Taken from lino_xl.lib.polls.PollResult)

Examples of writable virtual fields: - lino_xl.lib.ledger.DcAmountField - lino_xl.lib.polls.AnswersByResponseEditor.remark is a lino_xl.lib.polls.AnswerRemarkField - lino_xl.lib.families.CoupleField - lino_xl.lib.cal.ExtAllDayField - lino_xl.lib.excerpts.Excerpt.body_template_content is a lino_xl.lib.excerpts.BodyTemplateContentField - lino.core.mti.EnableChild


Some entries to the API:


  • Write a simple example project in tutorials

Some edge cases

A cool example is in lino_welfare.modlib.pcsw.models where we have:

dd.update_field(Client, 'overview', verbose_name=None)

This is special because Client is abstract at this place1. Abstract models don't have a copy of each inherited virtual field. the overview field is


Note that actually it is abstract only in eupen, not in chatelet. But that's another cool thing.