Welcome | Get started | Dive into Lino | Contribute | Topics | Reference | More

About virtual fields

virtual field

A data element whose value is 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.

Recipes

Define a virtual field on a model:

@dd.virtualfield(models.IntegerField(_("Bus")))
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:

@dd.virtualfield(dd.ForeignKey('polls.Question'))
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

Reference

Some entries to the API:

TODO:

  • 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 place[1]. Abstract models don't have a copy of each inherited virtual field. the overview field is